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
dboonepesdboonepes 

System.NullPointerException: Attempt to de-reference a null object

I keep getting the following error message when I try to insert a new record

 

System.NullPointerException: Attempt to de-reference a null object 

 

Class.StageStations.save: line 27, column 1

 

Here is the visualforce code

 

<apex:page controller="StageStations">
 <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>
  <apex:sectionHeader title="Add Station for {!account.name}"/>
    <apex:form >
      <apex:pageBlock title="Customer Information" mode="edit">    
        <apex:pageBlockButtons >
          <apex:commandButton action="{!save}" value="Save"/>
          <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>
      <apex:pageBlockSection title="Asset Information for {!asset.id}">

        <!-- Within a pageBlockSection, inputFields always display with their
             corresponding output label. --> 
    
        <apex:inputField id="assetid" value="{!asset.Station_ID__c}"/>
        <apex:inputField id="assetsn" value="{!asset.SerialNumber}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 Here is the Apex Class

public class StageStations {


    Account account;
    Asset asset;
    
    public Account getAccount() {
        return [select id, name,Contracted_Stations__c from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 
    } 
    
    public Asset getAsset() {
      if(asset == null) asset = new asset();
      return asset;
   }


    public PageReference cancel() {
        pageReference accountpage = new ApexPages.StandardController(account).view();
        accountpage.setRedirect(true);
        return accountpage; 
    }


    public PageReference save() {
    
      asset.AccountID = account.id;
      asset.Name = 'PES Station';
      insert asset;
        
        
      PageReference acctPage = new ApexPages.StandardController(account).view();
      acctPage.setRedirect(true);

      return acctPage;
    }
}

 I am pretty much a neophyte to Apex coding and I based this code on tutorials I found on the force.com site. Any insight would be appreciated.


Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

Btw, since Apex is case insensitive NEVER name your variables the same name as an SObject or class.

Change 'Account account' to 'Account myAccount' or something similar.  Same with 'Asset asset' to 'Asset myAsset'.

 

Your actual current problem is that you are never setting your account variable.  

 

public Account getAccount()
{
  myAccount = [select id, name,Contracted_Stations__c from Account         where id =: ApexPages.currentPage().getParameters().get('id')]; 
  return myAccount;
}

 Or better yet:

public class StageStations
{
    Account myAccount{get; private set;}
    Asset myAsset;

    public StageStations()
    {
        myAccount = [select id, name,Contracted_Stations__c from Account
                    where id =: ApexPages.currentPage().getParameters().get('id')];
    }
    
    public Asset getAsset()
    {
      if(myAsset == null) myAsset = new Asset();
      return myAsset;
    }

    public PageReference cancel()
    {
        PageReference accountPage = new ApexPages.StandardController(myAccount).view();
        accountPage.setRedirect(true);
        return accountPage; 
    }


    public PageReference save()
    {
      myAsset.AccountID = myAccount.id;
      myAsset.Name = 'PES Station';
      insert myAsset;
      
      PageReference acctPage = new ApexPages.StandardController(myAccount).view();
      acctPage.setRedirect(true);

      return acctPage;
    }
}

All Answers

Sam27Sam27

Hi,

 

As far as I can guess the account.id is not working which you are assigning to asset.AccountID..........most probably the account is not getting initialized so the account object is still null and when you are trying to get id out of it that is actuallly de-referencing a null object.

 

I am still not sure why it is not able to why the account isn't getting initialized....maybe it would be a better idea to initialize in a constructor.

just for checking purpose try to debug account by putting system.debug("Account----"+account); before where you are assigning the id and check the logs in debug log section to assure if that is the real problem.

 

Hope that helps in some way.

Jake GmerekJake Gmerek
I am still not sure why it is not able to why the account isn't getting initialized....maybe it would be a better idea to initialize in a constructor.

There is actually nowhere in the code where account is initialized.  You do need a constructor probably something like this:

public StageStations(){

account = [select id, name,Contracted_Stations__c from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 

}

 

but I can not be sure without knowing which account you actually want to set there.  As a side note you can then change this:

 

public Account getAccount() {
        return [select id, name,Contracted_Stations__c from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 
    } 

 to this:

 

public Account getAccount() {
        return account;
    } 

 

Will save you a SOQL query.

Damien_Damien_

Btw, since Apex is case insensitive NEVER name your variables the same name as an SObject or class.

Change 'Account account' to 'Account myAccount' or something similar.  Same with 'Asset asset' to 'Asset myAsset'.

 

Your actual current problem is that you are never setting your account variable.  

 

public Account getAccount()
{
  myAccount = [select id, name,Contracted_Stations__c from Account         where id =: ApexPages.currentPage().getParameters().get('id')]; 
  return myAccount;
}

 Or better yet:

public class StageStations
{
    Account myAccount{get; private set;}
    Asset myAsset;

    public StageStations()
    {
        myAccount = [select id, name,Contracted_Stations__c from Account
                    where id =: ApexPages.currentPage().getParameters().get('id')];
    }
    
    public Asset getAsset()
    {
      if(myAsset == null) myAsset = new Asset();
      return myAsset;
    }

    public PageReference cancel()
    {
        PageReference accountPage = new ApexPages.StandardController(myAccount).view();
        accountPage.setRedirect(true);
        return accountPage; 
    }


    public PageReference save()
    {
      myAsset.AccountID = myAccount.id;
      myAsset.Name = 'PES Station';
      insert myAsset;
      
      PageReference acctPage = new ApexPages.StandardController(myAccount).view();
      acctPage.setRedirect(true);

      return acctPage;
    }
}
This was selected as the best answer
craigmhcraigmh

Yeah, you're only referencing the local Account variable (account).

 

You never reference the method to initialize that variable, getAccount().

dboonepesdboonepes

Thanks for the help with this. This worked the way I expected now.

 

A quick question on this though...

 

The visualforce page displayed correct information about the account record I was trying to references. How is it possible that the variable had the correct information for the visualforce page, but not for inserting the account.id in the asset record?