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
Jeff BomarJeff Bomar 

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

Any help would be great. I created a trigger the before update works but if I add Before insert I get a NullPointerException: not sure where to go I have added the system.debug in every block of code but bot sure what I am looking at I am very new to Apex triggers.

Code:
trigger CalculateBalanceOfCustom on Custom_Programming__c (before insert, before update) 
{
   
    //SQL statement to lookup price 
    List<PricebookEntry> Pbe = [SELECT UnitPrice FROM PricebookEntry WHERE Product2.Name IN('Custom Development','Tape Drop')AND PriceBook2.Name='DAKCS'];
    
    //New Values for loop
    for (Custom_Programming__c cp:Trigger.new){
        
       //Old record values  
       Custom_Programming__c oldcp = Trigger.oldMap.get(cp.ID);
        
        //If total hours or custom type has not changed then bail    
       if(cp.Total_Hours__c==null || (cp.Total_Hours__c==oldcp.Total_Hours__c && cp.Custom_Type__c == oldcp.Custom_Type__c))
           continue;

        //Tapedrop
       if (cp.Custom_Type__c == 'Tape Drop') 
        {
            cp.Testing_Balance__c = pbe[1].UnitPrice + (pbe[0].UnitPrice * cp.Total_Hours__c);
        }
        //No Billing Bug Fix
       else if (cp.Custom_Type__c =='No Billing' || cp.Custom_Type__c =='Bug Fix')
        {
          cp.Testing_Balance__c = 0;
        }
        //Everything Else
       else 
        {
           cp.Testing_Balance__c =pbe[0].UnitPrice * cp.Total_Hours__c;
        }        
    }
}
 
Best Answer chosen by Jeff Bomar
AbhishekAbhishek (Salesforce Developers) 
“System.NullPointerException: Attempt to de-reference a null object”
This is a very common error message which occurs when your object/list is not initialized (allocated memory). So when say not initialized means the following:

In order to use the non-primitive data type in the code, we have initialized the memory to those data types. For example:

I want to use the Account object in my apex class, so I have allocated memory to that object. So to do that I have to write:

Account accountObj = new Account(); // allocating memory to the account.

Similarly in case of list:

list<Account> accountList  = new list<Account>(); // allocating memory to list of accounts.

So in your code make sure you have allocated the memory to your non-primitive data types, else you will ran into the same error.

Try to initialize the list or object.

For example:

Public List<Account> acc {get;set;}
acc = new List<Account>(); 
Likewise whenever you use this list variable "acc". Try to check size before you do an update or iteration.
Example:
if(!accList.isEmpty()) {
//Your code logic for accList.
}

Ref: http://npntraining.com/blog/2017/best-practices-to-avoid-nullpointerexception/


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.

All Answers

AbhishekAbhishek (Salesforce Developers) 
“System.NullPointerException: Attempt to de-reference a null object”
This is a very common error message which occurs when your object/list is not initialized (allocated memory). So when say not initialized means the following:

In order to use the non-primitive data type in the code, we have initialized the memory to those data types. For example:

I want to use the Account object in my apex class, so I have allocated memory to that object. So to do that I have to write:

Account accountObj = new Account(); // allocating memory to the account.

Similarly in case of list:

list<Account> accountList  = new list<Account>(); // allocating memory to list of accounts.

So in your code make sure you have allocated the memory to your non-primitive data types, else you will ran into the same error.

Try to initialize the list or object.

For example:

Public List<Account> acc {get;set;}
acc = new List<Account>(); 
Likewise whenever you use this list variable "acc". Try to check size before you do an update or iteration.
Example:
if(!accList.isEmpty()) {
//Your code logic for accList.
}

Ref: http://npntraining.com/blog/2017/best-practices-to-avoid-nullpointerexception/


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
This was selected as the best answer
Jeff BomarJeff Bomar
Do I need to rewrite this trigger is it written wrong!
AbhishekAbhishek (Salesforce Developers) 
Yes, You have to change the trigger based on my suggestions.
Jeff BomarJeff Bomar
Okay i will try to give it a shot really new to this and i think I have watched about a 100 videos.
Jeff BomarJeff Bomar
could you tell me if I should be using before insert, before update, or after insert after update update.
AbhishekAbhishek (Salesforce Developers) 
Use before insert, before update.

If it helps you and closes your query by marking it as solved so that it can help others in the future.

Thanks.