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
gnevegneve 

Test Class failing with "Attempt to de-reference null object" error

Hello,

 

I have two triggers that pull information from a completed task, one puts that information on a contact record, one puts that information on a lead record based on the WhoId in the task record. The two triggers are written to be exactly the same. Below is the contact trigger.

 

trigger ContactLastCallResult on Task (after insert,after update)
{
String TSubject='Call';
String TResult='Completed';
String WType='003';
Map<Id, Contact> ContactIDMap = new Map<Id, Contact>();
Set<id> Ids = new Set <id>();
for (Task tk: Trigger.new)
{
Ids.add(tk.WhoId);
}
Map<id, Contact> ContactIDMap2 = new Map<id, Contact>([SELECT Id FROM Contact WHERE Id in :Ids]);
for (Task t: Trigger.new)
if (t.subject.contains(TSubject) && t.status.contains(TResult) && t.WhoType__c.startsWith(WType))
{
Contact c = ContactIDMap2.get(t.WhoId);
c.Last_Call_Result__c = t.CallDisposition;
c.Last_Call_Date__c = t.Completed_Date__c;

ContactIDMap.put(c.id,c);
}
update ContactIDMap.values();
}

 

I put together a simple test class to simulate a task being created:

@isTest
private class WhoType_Test {

static TestMethod void Test0_TestCTaskInsert()
{
Task t = new Task();
t.Subject = 'Call';
t.whoid = '003f0000009ApEU';
t.calldisposition = 'contact';
t.status = 'completed';
t.completed_date__c = t.LastModifiedDate;
insert t;     
}
}

 

When I run the test, I get:

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

Trigger.ContactLastCallResult: line 17, column 1

 

Line 17 is:

c.Last_Call_Result__c = t.CallDisposition;

 

Anyone have any ideas?

 

Thanks,


Gus

 

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Try below approach to see if it resolves. But I would recommend creating a new contact in your test class and then task.

 

@isTest (seealldata = true)

private class WhoType_Test {

static TestMethod void Test0_TestCTaskInsert()
{
Task t = new Task();
t.Subject = 'Call';
t.whoid = '003f0000009ApEU';
t.calldisposition = 'contact';
t.status = 'completed';
t.completed_date__c = t.LastModifiedDate;
insert t;     
}
}

 

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Try below approach to see if it resolves. But I would recommend creating a new contact in your test class and then task.

 

@isTest (seealldata = true)

private class WhoType_Test {

static TestMethod void Test0_TestCTaskInsert()
{
Task t = new Task();
t.Subject = 'Call';
t.whoid = '003f0000009ApEU';
t.calldisposition = 'contact';
t.status = 'completed';
t.completed_date__c = t.LastModifiedDate;
insert t;     
}
}

 

This was selected as the best answer
gnevegneve

Thanks you. Can you explain what is happening here? I am working on rebuilding all of my test classes to create new contacts, leads, accounts, and opportunities to test these triggers, but I would like to know why this change fixed my issue.

 

Thanks,

 

Gus

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

The issue lies on this line..

 

t.whoid = '003f0000009ApEU';

 


All the test classes created with version  greater than 24.0 will not consider the data that is already there. You will have to explicitly create test data. 

 

Once you specify seealldata = true it means that the test class has access to the contact 003f0000009ApEU else it wouldn't.

 

For more information look at this link..

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm

gnevegneve

That makes sense. Thanks. I am against a time crunch needing to get a set of triggers deployed, but I will be going back through to rebuild test classes that follow best practice and do not hard code the record id values.

 

Thanks again.

 

Gus