You need to sign in to do that
Don't have an account?
Need help in : System.NullPointerException: Attempt to de-reference a null object
I have Bigmachine Custom Object
In this i have 3 Fields
1. Scope__c
2. Scope1__c
3. Scope2__c.
Scope1__c and Scope2__c are Formula fields and Scope__c field is Number and is summation of Scope1__c and Scope2__c.
Below trigger gives me proper Scope__c values.
Trigger generateFields on Bigmachine__c (before insert,before update) {
Bigmachine__c big = Trigger.New[0];
big.Scope__c=big.Scope1__c-der.Scope2__c;
}
When I am writing Test class for this Trigger I am getting this :
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, generateFields: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.generateFields: line 3, column 1: []
My Test class is:
@isTest(SeeAllData=true)
public with sharing class generateFieldsTest {
static testMethod void validatefields(){
Bigmachine__c big = new Bigmachine__c();
list<Bigmachine__c> bg = [select Scope1__c, Scope2__c from Bigmachine__c];
for(Bigmachine__c b : bg){
if(b.Scope1__c !=null && b.Scope2__c != null){
big.Scope__c = b.Scope1__c - b.Scope2__c;
if(big.Scope__c > 0){
try{
insert big;
}catch(dmlexception de){
system.debug('!!!!!!' +de.getmessage());
throw de;
}
}
}
}
}
}
I am getting somehow code coverage but my test method is getting failed.
Can somebody please help me out to pass my test class and get the proper code coverage
Thanks in advance!!!!!!!!
Hi Sri,
The error occurs when code tries to reference or calculate on null data.
See the below links for related discussions,
http://boards.developerforce.com/t5/Apex-Code-Development/caused-by-System-NullPointerException-Attempt-to-de-reference-a/td-p/272463
http://208.74.204.95/t5/Apex-Code-Development/System-NullPointerException-Attempt-to-de-reference-a-null/td-p/635251
Regards,
Ashish
In test class you have to insert the fileds which is used to calculate Scope1__c and Scope2__c
for ex
if Scope1__c = A +B and Scope2__c = c + d
then your test class will be
@isTest(SeeAllData=true)
public with sharing class generateFieldsTest {
static testMethod void validatefields(){
Bigmachine__c big = new Bigmachine__c();
big.A=2;
big.B=2;
big.c=2;
big.d=2;
insert big;
}
}
Thanks for quick reply Swetha!!!!
But in my scenario Scope1__c and Scop2__c are being calculated with formula fields and again those fields are being calculated with some other formula fields, so I can't pass test/dummy values to that fields.
I think i have the problem in my trigger i.e trigger event.
Hi Sri,
Currently the formula fields are displayed only when we open the record in the UI or query the record.
Vote for the idea:
https://success.salesforce.com/ideaview?id=08730000000BrIGAA0
You may do the Formula calculation of the Field in your Trigger itself and pass on the value.
Or
(1) Don't use a formula field, but use a regular field and a workflow field update that replicates the formula. That way the field will be stored in the database.
(2) Replicate the formula in your workflow rule and fire it every time a record is created or edited.
Regards,
Ashish
Thanks for the reply Ashish!!!
As per my requirement, i should not change my trigger for the sake of Test class as it is working properly and giving me expected values i.e Scope.
And i am utilizing the trigger with before insert, before update event and it is working fine.
But failing in test class says null pointer exception. can you walk into this aspect.
Thank you
Hi Sri,
In the Test class, instead of referring to Scope__c, can you use Scope__1 + Scope_2.
Regards,
Ashish
Hi sri
I think you have to paas dummy value for all the filed which are used to calculate 1st formula fields.
for ex
Scope1__c = A+B
and A = C+D
B= E +F
then you have to pass value for C , D , E and F