You need to sign in to do that
Don't have an account?

BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object
Hi,
I am finding accumulated sum in some objects.My logic is worked in some objects.In some other objects the following error is raising.
Review all error messages below to correct your data.
Apex trigger
chiranjeevi.mbltotal caused an unexpected exception, contact your
administrator: chiranjeevi.mbltotal: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null
object: Trigger.chiranjeevi.mbltotal: line 10, column 15
my code is as follows:
trigger mbltotal on chiranjeevi__Sea_Foot_Note__c (before insert,before update) {
Double objBtotal = 0;
List<chiranjeevi__Master_Bill_Of_Ladding__c> objAlist=new List<chiranjeevi__Master_Bill_Of_Ladding__c>();
for(chiranjeevi__Sea_Foot_Note__c b : Trigger.new)
objAlist=[SELECT chiranjeevi__Console_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Master_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: b.Id];
for (chiranjeevi__Sea_Foot_Note__c objA : Trigger.new)
{
for (chiranjeevi__Master_Bill_Of_Ladding__c objB : objAlist)
objBtotal += objB.chiranjeevi__Console_charges__c;
objA.chiranjeevi__Total_service_charges__c=objBtotal;
}
}
If any one find out the solution plz help me.
Thanks in advance,
Anu..
The error occurs when code tries to reference or calculate on null data.
I have refined the code again as follows:
trigger mbltotal on chiranjeevi__Sea_Foot_Note__c (before insert,before update) { for(chiranjeevi__Sea_Foot_Note__c objA : Trigger.new) { Double objBtotal = 0; for(chiranjeevi__Master_Bill_Of_Ladding__c objBlist:[SELECT chiranjeevi__Console_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Master_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: objA.Id]) { if(objBlist.chiranjeevi__Console_charges__c != null) objBtotal += objBlist.chiranjeevi__Console_charges__c; } if(objBtotal != 0) objA.chiranjeevi__Total_service_charges__c=objBtotal; } }
All Answers
I tried refining the code in order to handle the NullpointerException as follows:
trigger mbltotal on chiranjeevi__Sea_Foot_Note__c (before insert,before update)
{
for(chiranjeevi__Sea_Foot_Note__c objA : Trigger.new)
{
Double objBtotal = 0;
for(chiranjeevi__Master_Bill_Of_Ladding__c objBlist:[SELECT chiranjeevi__Console_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Master_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: objA.Id])
{
objBtotal += objBlist.chiranjeevi__Console_charges__c;
}
if(objBtotal != 0) objA.chiranjeevi__Total_service_charges__c=objBtotal;
}
}
Can you please try and let me know if it works.
The error occurs when code tries to reference or calculate on null data.
I have refined the code again as follows:
trigger mbltotal on chiranjeevi__Sea_Foot_Note__c (before insert,before update) { for(chiranjeevi__Sea_Foot_Note__c objA : Trigger.new) { Double objBtotal = 0; for(chiranjeevi__Master_Bill_Of_Ladding__c objBlist:[SELECT chiranjeevi__Console_charges__c,chiranjeevi__SFN_no__c FROM chiranjeevi__Master_Bill_Of_Ladding__c Where chiranjeevi__SFN_no__c =: objA.Id]) { if(objBlist.chiranjeevi__Console_charges__c != null) objBtotal += objBlist.chiranjeevi__Console_charges__c; } if(objBtotal != 0) objA.chiranjeevi__Total_service_charges__c=objBtotal; } }
Thanks for ur reply,
Now it is working.but at the time of creating a record in new mode with new SFN Number in Sea Foot Note Object some amount is stored in Total service Charges inputfield.That amount is related to previouse MBL console charges.plz tell me how to solve this problem.
Thanks in advance,
Anu..
You can try giving some default value to this field, so that its never without a value.
I did this and it worked.
Thanks,
trigger duplicateaccount on Account (before insert, after insert) {
List<Account> st = new List<Account>();
st = [SELECT CreatedDate FROM Account ORDER BY CreatedDate DESC NULLS LAST LIMIT 1];
for(Account a: Trigger.new){
for(Account acc:st){
System.debug('createddate' + acc.CreatedDate);
Datetime firsttime = System.now();
System.debug('first time' + firsttime);
Datetime secondtime = Datetime.valueOf(acc.CreatedDate);
System.debug('Secondtime' + secondtime);
Decimal millisecondsBetween = firstTime.getTime() - secondTime.getTime();
System.debug(millisecondsBetween);
Decimal timeBetween = millisecondsBetween / 3600000;
decimal timediff = timeBetween.setscale(0);
System.debug('diif'+timediff);
if(timediff > 1) {
for(Account temp: st){
Account objAccount = Trigger.newMap.get(temp.id);
objAccount.addError('one hour');
}
}
}
}
}
Im getting error like
uplicateaccount: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.duplicateaccount: line 21, column 1: [] can anyone help in this.