function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JComplianceJCompliance 

Attempt to de-reference a null object - HELP

Greetings All -

 

I am getting the above error when trying to run the following code utilizing a VF Page Buton.

 

VF Page Code:

<apex:page standardController="Protocol_Arm__c"
	extensions="ProtocolScheduleClone"
	action="{!cloneWithItems}">
	<apex:pageMessages />
</apex:page>

 

Class Code:

public class ProtocolScheduleClone {

    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
    // add the instance for the variables being passed by id on the url
    private Protocol_Arm__c pa {get;set;}
    // set the id of the record that is created 
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public ProtocolScheduleClone(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        pa = (Protocol_Arm__c)controller.getRecord();
 
    }
 
    // method called from the VF's action attribute to clone the po
    public PageReference cloneWithItems() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Protocol_Arm__c newPA;
 
         try {
 
              //copy the protocol arm 
             pa = [select Id, Name, Description__c, Protocol__c, Start_Date__c from Protocol_Arm__c where id = :pa.id];
             newPA = pa.clone(false);
             insert newPA;
 
             // set the id of the new pa created for testing
               newRecordId = newPA.id;
  
             // copy over the line items 
             List<Arm_Activity__c> items = new List<Arm_Activity__c>();
             for (Arm_Activity__c aa : [Select aa.Id, aa.Activity_Type__c, aa.Description__c, aa.Protocol_Arm__c, aa.Originally_Scheduled_Date_Time__c, aa.Title__c From Arm_Activity__c aa where Protocol_Arm__c = :pa.id]) {
                  Arm_Activity__c newARA = aa.clone(false);
                  newARA.RecordType.Name = 'Actual';
                  newARA.Protocol_Arm__c = newPA.id;
                  items.add(newARA);
             }
             insert items;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }
 
        return new PageReference('/'+newPA.id+'/e?retURL=%2F'+newPA.id);
    }
 



}

 Any Help would be appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab

As far as I know, you can't set it by name.

 

Here is an example (was working, but modified for your name):

RecordType RecordTypeID = [
                SELECT Id, Name 
                FROM RecordType 
                WHERE Name = 'Actual' 
                and SobjectType = 'Protocol_Arm__c' limit 1
                ];

After this code, you can use RecordTypeID.id to get the id like this:

newARA.RecordTypeId = RecordTypeID.id;

Hope this helps ...

All Answers

colemabcolemab

From the log, please tell us what line in your code is throwing this error

 

or

 

post the log ...

JComplianceJCompliance

How can I export the log to display it?

colemabcolemab

First you will need to capture the log:

  1. Login to salesforce
  2. In the upper right hand corner is your name, click that to get a menu.
  3. In that menu, click setup and you will be taken to a new page. 
  4. On the new page, look at the left hand menu and locate "Administration setup".  Under that, click "Monitoring".
  5. Under "Monitoring, click "Debug Logs".
  6. Click the "New" button under the Monitored users section.
  7. Use the text box and/or search screen to select the user who will be clicking the button (most likely you) and click save.
  8. In a new window or tab, click the button to generate the error.
  9. Return to the debug logs window and refresh the page.  Locate the monitored user you setup and click delete to stop capturing logs.
  10. Look at the debug logs section (bottom half of page), you might have a couple of logs here but locate the one that shows the exception where you attempted to deference a null object.
  11. Use the download link to save the log.   If the log is over 20k characters, please copy and paste the relevant line(s) back to this thread only.

Hope this helps.

 

JComplianceJCompliance

Thank you for that direction. I did however figure out the issue. In the line where I am attempting to insert the RecordType name, there are some additional issues I am having there. Without that line th code is solid. I will reply with what I find out on the issue.

colemabcolemab

Reply with the offending line of code and I will look at it . . . .

JComplianceJCompliance

It is in this line that I am setting the record type:

 

newARA.RecordType.Name = 'Actual';

 

colemabcolemab

As far as I know, you can't set it by name.

 

Here is an example (was working, but modified for your name):

RecordType RecordTypeID = [
                SELECT Id, Name 
                FROM RecordType 
                WHERE Name = 'Actual' 
                and SobjectType = 'Protocol_Arm__c' limit 1
                ];

After this code, you can use RecordTypeID.id to get the id like this:

newARA.RecordTypeId = RecordTypeID.id;

Hope this helps ...

This was selected as the best answer
JComplianceJCompliance

I understand the above code and why to use it, but I am not sure where to put it. My assumption is that it will not go into the For loop correct?

JComplianceJCompliance

Figured it out! Thank you so much for your hepl!

colemabcolemab

No problem.

 

If you get bored, please check out my salesforce blog here.

 

If you like it and/or my posts helped you, you can always nominate me for MVP here. :smileyvery-happy: