+ Start a Discussion
AndreyAndrey 

NullPointerException de-reference a null object when I try to save

Hi all!
I created vf page and  when I try to save a new record, I get an error: "System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!Save}' in component <apex:commandButton> in page te_new". 
Can someone tell me, where I'm wrong?
Vf page code:
<apex:page standardController="Travel_Expenses__c" extensions="TE_Object">
    <apex:form >
        <apex:pageBlock title="Travel Information Edit"  mode="edit">
            <apex:pageBlockButtons >
                    <apex:commandButton action="{!Save}" value="Save"/>
                    <apex:commandButton action="{!SaveAndNew}" value="Save & New"/>
                    <apex:commandButton action="{!Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Information" columns="2" >
                <apex:pageBlockSectionItem >
                    Travel Information
                    <apex:inputField value="{!NewTE.Traveler__c}" required="true"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Expense Type
                    <apex:inputField value="{!NewTE.Expense_Type__c}" required="true"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Amount
                    <apex:inputField value="{!NewTE.Amount__c}" required="true"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Country
                    <apex:inputField value="{!NewTE.Country__c}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Inventory Number
                    <apex:inputField value="{!NewTE.Inventory_Number__c}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    Description
                    <apex:inputField value="{!NewTE.Description__c}" required="true"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
apex class:
public class TE_Object 
{
    public Travel_Expenses__c NewTE{get;set;}
    public TE_Object(ApexPages.StandardController controller) {

    }
    
    public pagereference Save()
    {
        try 
        {
            insert NewTE; //Error message on this line
        } 
        catch(System.DMLException e) 
        {
            ApexPages.addMessages(e);
            return null;
        }  
        return null;
    }
    public pagereference SaveAndNew()
    {
        try 
        {  
            insert NewTE; 
        } 
        catch(System.DMLException e) 
        {
            ApexPages.addMessages(e);
            return null;
        }   
        return (new ApexPages.StandardController(new Travel_Expenses__c())).edit();   
    }
    
}



Best Answer chosen by Andrey
Vinit_KumarVinit_Kumar
The reason why you are getting this error is because you haven't intialized you variable.Try the below apex clas :-

public class TE_Object 
{
    public Travel_Expenses__c NewTE{get;set;}
    public TE_Object(ApexPages.StandardController controller) {
       
      NewTE = new Travel_Expenses__c (); // Initializing the variable
    }
    
    public pagereference Save()
    {
        try 
        {
            insert NewTE; //Error message on this line
        } 
        catch(System.DMLException e) 
        {
            ApexPages.addMessages(e);
            return null;
        }  
        return null;
    }
    public pagereference SaveAndNew()
    {
        try 
        {  
            insert NewTE; 
        } 
        catch(System.DMLException e) 
        {
            ApexPages.addMessages(e);
            return null;
        }   
        return (new ApexPages.StandardController(new Travel_Expenses__c())).edit();   
    }
    
}
If this helps,please mark it as best answer to help others :)

All Answers

Vinit_KumarVinit_Kumar
The reason why you are getting this error is because you haven't intialized you variable.Try the below apex clas :-

public class TE_Object 
{
    public Travel_Expenses__c NewTE{get;set;}
    public TE_Object(ApexPages.StandardController controller) {
       
      NewTE = new Travel_Expenses__c (); // Initializing the variable
    }
    
    public pagereference Save()
    {
        try 
        {
            insert NewTE; //Error message on this line
        } 
        catch(System.DMLException e) 
        {
            ApexPages.addMessages(e);
            return null;
        }  
        return null;
    }
    public pagereference SaveAndNew()
    {
        try 
        {  
            insert NewTE; 
        } 
        catch(System.DMLException e) 
        {
            ApexPages.addMessages(e);
            return null;
        }   
        return (new ApexPages.StandardController(new Travel_Expenses__c())).edit();   
    }
    
}
If this helps,please mark it as best answer to help others :)

This was selected as the best answer
ShashForceShashForce
Hi Andrey,

Please change your constructor method as below and you should be good.

public TE_Object(ApexPages.StandardController controller) {
        this.NewTE = (Travel_Expenses__c)controller.getRecord();
    }

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
AndreyAndrey
Thanks for answers, both methods are working!