You need to sign in to do that
Don't have an account?
Jeff 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;
}
}
}
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;
}
}
}
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
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.
If it helps you and closes your query by marking it as solved so that it can help others in the future.
Thanks.