1

I am trying to update a field in a custom object and I have stripped back my code to the bare minimum. I am getting this error on submission when I click the save button. How do I fix it?

System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!save}' in component in page inputfieldhelloworld: Class.leadershipReadyCon.save: line 24, column 1

Class.leadershipReadyCon.save: line 24, column 1

Here is my APEX Class

public with sharing class leadershipReadyCon {

// After declaring final you can not modify any record.    
//public final Leadership_Ready__c myLeadershipReady;

// getter and setter required to access and update record field from page.
public Leadership_Ready__c myLeadershipReady {get;set;}
PageReference ref = new PageReference('&message=hello%20world');


 //Constructor function
public leadershipReadyCon() {
   //Initialize  first to avoid null error.
    Leadership_Ready__c myLeadershipReady  = new Leadership_Ready__c();

    // if there is any record found then it will assign to myLeadershipReady otherwise we have already Initialize it.
     for(Leadership_Ready__c  lr : [SELECT Id, Automated_TP_Status__c FROM Leadership_Ready__c  WHERE Id = :ApexPages.currentPage().getParameters().get('prodId')]){ 

    }

}// End Constructor function

public PageReference save(){  
    update myLeadershipReady;
    ref.setRedirect(true);
    return ref;  
} 

}

Here is the VFP

<apex:page controller="leadershipReadyCon" tabStyle="Leadership_Ready__c">

<apex:form >

    <apex:pageBlock title="Automated Training Plan Status">
       <!-- You belong to Account Name: <apex:inputField value="{!Leadership_Ready__c.Automated_TP_Status__c}"/>
             Always you your variable not the object as you can see in code below.
           -->
        Your Status: <apex:inputField value="{!myLeadershipReady.Automated_TP_Status__c}" />

        <apex:commandButton action="{!save}" value="save" />
    </apex:pageBlock>
</apex:form>

Thank you.

2

You can declared local variable as the same name in the Constructor

Leadership_Ready__c myLeadershipReady  = new Leadership_Ready__c();

That's why during save, class level myLeadershipReady variable is null.

Just corrected the code:

public with sharing class leadershipReadyCon {

    // After declaring final you can not modify any record.    
    //public final Leadership_Ready__c myLeadershipReady;

    // getter and setter required to access and update record field from page.
    public Leadership_Ready__c myLeadershipReady {get;set;}
    PageReference ref = new PageReference('&message=hello%20world');


     //Constructor function
    public leadershipReadyCon() {
       //Initialize  first to avoid null error.
        myLeadershipReady  = new Leadership_Ready__c();

        // if there is any record found then it will assign to myLeadershipReady otherwise we have already Initialize it.
         for(Leadership_Ready__c  lr : [SELECT Id, Automated_TP_Status__c 
            FROM Leadership_Ready__c  
            WHERE Id = :ApexPages.currentPage().getParameters().get('prodId')]){ 
            myLeadershipReady = lr;
        }

    }// End Constructor function

    public PageReference save(){  
        update myLeadershipReady;
        ref.setRedirect(true);
        return ref;  
    } 

}
  • Thanks Boral, that fixed that issue but now it is coming up with a different error: >System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Error is in expression '{!save}' in component <apex:commandButton> in page inputfieldhelloworld: Class.leadershipReadyCon.save: line 24, column 1 Class.leadershipReadyCon.save: line 24, column 1 – proteuscanvas Jul 27 '17 at 4:19
  • you need to structure the code properly. From the SOQL query you are retrieving Leadership_Ready__c record. Hopefully this record should be updated. correct? updated the code. Now properly structure your code – Santanu Boral Jul 27 '17 at 4:30
  • That is working now, it updates the object but I am still getting an error. Visualforce Error Formula Expression is required on the action attributes. – proteuscanvas Jul 27 '17 at 4:36
  • 1
    Yes you were very helpful. I think that the error is in this bit: PageReference ref = new PageReference('&message=hello%20world'); – proteuscanvas Jul 27 '17 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.