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
renu anamalla 3renu anamalla 3 

null pointer

what is the null pointer
 
Rupali PophaliyaRupali Pophaliya
Null pointer error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been initialized.
salesforce mesalesforce me
check this once....

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm?search_text=null%20pointer
Rupal KumarRupal Kumar
Hi renu,
   This error is caused by a line of code that is trying to use an object that has not been instantiated, or an object's attribute that has not been      initialized.

Example 1: If the get() method in the following code cannot find the value oldAccount.Name in the map accountMap, the object newAccount will be null, and trying to use it will generate this error message:
trigger SampleTrigger on Account (before insert, before update){
     
     integer i = 0; 
     
     Map<String, Account> accountMap = new Map<String, Account>{};

        for (Account acct : System.Trigger.new)

           accountMap.put(acct.Name, acct);

        for (Account oldAccount : [SELECT Id, Name, Site FROM Account WHERE Name IN :accountMap.KeySet()]) {

           Account newAccount = accountMap.get(oldAccount.Name);

       i = newAccount.Site.length;

       }
}

Good to know: If the field Site was left empty, trying to use it as above will generate the error message as well.
Resolving the issue
The solution is to make sure the Object and/or the Attribute to be used is not null. In this example, the code needs to be modified as follows:
Account newAccount = accountMap.get(oldAccount.Name);

        if (newAccount != null) 

          if (newAccount.Site != null) 

            i = newAccount.Site.length();

/**********OR*****

An exception handling routine can be used, such as:


Account newAccount = accountMap.get(oldAccount.Name);

        try {

             i = newAccount.Site.length();

            }
            catch (System.NullPointerException e) {

            e1 = e; // can be assigned to a variable to display a user-friendly error message

        }


Thanks,
Rupal Kumar.
http://www.mirketa.com