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
developer srideveloper sri 

Need help in : System.Nul​​lPointerE​x​ception: Attempt to de-referen​​ce 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!!!!!!!!

Sagarika RoutSagarika Rout

You are getting error in trigger not in test class.

The reason you are getting this  is

 

1. first of all you have written the trigger in before insert , In before insert the field scope1__c and scope2__c will carry

NULL values(as these are formula).

2. You are trying to update a number field (Scope__c) with NULL values. thats why it is saying System.NullPointerException: Attempt to de-reference a null object on line no 3.

 

I think it will help you out to come up with a solution.

 

 

regards

Sagarika Rout

SFDC Developer

developer srideveloper sri

Thanks for quick reply Sagarika!!!!

 

Thanks for finding out my code faults.

 

But my Trigger is working fine i.e it is giving me proper values while creating Bigmachine records and updating the same.

 

Can you help me in this

 

 

Thanks!!!!

Sagarika RoutSagarika Rout

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: []

 

 

This shows your trigger is giving the error,  I mean to say while running the test class you are tring to insert some dummy data to the object, at that time your trigger is firing (as you have written before insert) and it is giving error.

 

This sinario i have tried also and i am getting the same error.I am not able to understand how it is working fine while insert. As formula fields are read only field and you will only get the values after insert.


Please verify again.

 

 

 

Sagarika Rout

SFDC Developer

developer srideveloper sri

Yeah!!! somewhere might be the problem with trigger event in my code.

 

I am cheking it once again!! I will come back soon

 

Thanks!!!!

Sagarika RoutSagarika Rout

If  the two formula field contain some constant or hard coded values , then your trigger will not throw error,

but it will not update that perticular field.

 

Might be for this reason you are not getting error while inserting.

 

One simple way to cover your trigger is

Insert one record and update the same record in your test class. the trigger will autometicly fire and it will cover.

No need to give all these condition in test class, as you are not providing in side the trigger.

 

 

 

Sagarika Rout

SFDC Developer

developer srideveloper sri

HI!!!!

 

In my trigger Scope1 and Scope2 fields are not harcoded or constant values and my trigger is not throwing any error when it fires.

 

and as you mentioned inorder to insert one record and update the same i can't assing test values to Scope or scope1 or scope2 as they are formula fields.

 

Can you share the code which you are expecting me to do

 

Thank you!!!