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
lizmijohnlizmijohn 

Test class error help

I have a unit test as follows:

 On running test i am getting the following error: "System.NullPointerException: Attempt to de-reference a null object"

 

Not able to figure out where exactly the problem is. Please help!

 



public class projecttrigger { 
    static testMethod void ProjectAnalysistrigger() {
    Project1__c prj = new Project1__c();
    prj.name='P-10000';
    prj.Project_Name__c='test project';
    prj.Country__c='a0GL00000009Ydl';
    prj.End_Market_Segment__c='Storage';
    prj.Start_Date__c=Date.newInstance(2011,6,1);
    prj.EndDate__c=Date.newInstance(2012,6,1);
    prj.Analysis_dell__c='a0CL00000004ijw';
    prj.ProjectValue__c=50000;
    
    insert prj;
     Project_Analysis__c[] analysisToCreate = new Project_Analysis__c[]{};
        
        Integer i=0;
        date Date1= prj.Start_Date__c;
        date myDate1=Date1;
    
        if (i <= (prj.No_of_months__c + 1)) {
        for (myDate1 = myDate1; myDate1 < prj.EndDate__c.addDays(25); myDate1= myDate1.addDays(31)) {
        analysisToCreate.add(new Project_Analysis__c(Project1__c= prj.Name,Amount_per_month__c= prj.Project_Value_per_month__c ,Date__c = myDate1,Start_Date__c=prj.Start_Date__c));
        }
        i=i+1;
        }
               


        Test.startTest();

        insert analysisToCreate;

        Test.stopTest();   

    }  

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz

Ah, formula fields are not calculated until insertion into the DB, and the object is not updated with those new values.  You'll have to query the record again after inserting it to ensure that your formula fields are populated. 

All Answers

spraetzspraetz

     if (i <= (prj.No_of_months__c + 1)) {

 

I don't see No_of_months being set anywhere, it's probably equal to null and when you try to add 1 to it... NPE.

lizmijohnlizmijohn

No_of_months__c is a formula field. Can I set it anywhere, as it gives an error that this field is not writable?

spraetzspraetz

Ah, formula fields are not calculated until insertion into the DB, and the object is not updated with those new values.  You'll have to query the record again after inserting it to ensure that your formula fields are populated. 

This was selected as the best answer
lizmijohnlizmijohn

Can u help me with that? If required I can show my code through a team viewer session. Please let me know.

spraetzspraetz

after insert prj;

 

You'll want to do prj = [SELECT id, name, project_name__c, country__c, end_market_segment__c, start_date__c, endDate__c, analysis_dell__c, projectValue__c, No_of_months__c, project_value_per_month__c FROM Project1__c WHERE id = :prj.id];

lizmijohnlizmijohn

I have country_c as a lookup field in Project page. I have initialized the country_c as follows:

 

prj.country_c ='a0GD000000U6Y8P';

 

On deploying the test failed and it gave the following error:

 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [Country__c]", Failure Stack Trace: "Class.projecttrigger.ProjectAnalysistrigger: line 18, column 9 External entry point"

 

Please help.

lizmijohnlizmijohn

I could figure out the mistake that i was doing. For country_c I gave the id value of country record in sandbox. When it is deployed it was not detecting the record id, as it differs in production environment.