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
NickDevNickDev 

Test Case error: Attempt to de-reference a null object

I've started writing a Test Case so I can deploy my trigger. These are both for custom objects, however these are also on a managed package.

I am getting the following error when executing my test case:
 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, pse.handleTimecardHeaderChange: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

I assume there is a perticular field which is used in an existing Trigger called pse.handleTimecardHeaderChange. However it has not been made mandatory, therefore the field is null?

If I am correct, how can I go about troubleshooting this as when I create the record through the Page it works fine and I cannot see any other fields that are populated by default.

Thanks
JaiChaturvediJaiChaturvedi
Can you provide code snippet?
NickDevNickDev
Sorry, yes... early stages yet so this is all I have for the test case so far:
 
@isTest
public class InsertTimecardTest {
    static testmethod void insertTimecard(){
        pse__Timecard_Header__c t = new pse__Timecard_Header__c();
        t.pse__Project__c = 'a1V58000000UmsT';
        t.pse__Resource__c = '00358000001pNAP';
        t.pse__Start_Date__c = Date.newInstance(2016, 6 , 20);
        t.pse__End_Date__c = Date.newInstance(2016, 6 , 26);
        t.pse__Assignment__c = 'a0g3E0000008vuR';
        t.CurrencyIsoCode = 'GBP';
        t.pse__Monday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Tuesday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Wednesday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Thursday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Friday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Saturday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Sunday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Billable__c = true;
        insert t;
    }
}

 
JaiChaturvediJaiChaturvedi
the issue seems to be in handleTimecardHeaderChange() method. Can you provide that?
NickDevNickDev
Its a Trigger, unfortunately a managed package one which has been hidden... I am unable to view the code for it. Perhaps I may have to speak to the vendor.
sailee handesailee hande
Hi Nick,

Try this one.
Instead of putting ids for lookup create that object in test class and then give that object's id.

@isTest
public class InsertTimecardTest {
    static testmethod void insertTimecard(){
        pse__Project__c project = new pse__Project__c (Name='ABC');
        pse__Resource__c resource = new pse__Resource__c (Name='ABC');
        pse__Assignment__c assignment = new .pse__Assignment__c (Name='ABC');
        insert project;
        insert resource;
        insert assignment;


        pse__Timecard_Header__c t = new pse__Timecard_Header__c();
        t.pse__Project__c = project.id;
        t.pse__Resource__c = resource.id;

        t.pse__Start_Date__c = Date.newInstance(2016, 6 , 20);
        t.pse__End_Date__c = Date.newInstance(2016, 6 , 26);
        t.pse__Assignment__c = assignment.id;
        t.CurrencyIsoCode = 'GBP';
        t.pse__Monday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Tuesday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Wednesday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Thursday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Friday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Saturday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Sunday_Hours__c = Decimal.valueOf('5.00');
        t.pse__Billable__c = true;
        insert t;
    }
}

Regards,
Sailee