+ Start a Discussion
Rachit Kumar JainRachit Kumar Jain 

I am getting this error NullPointerException when i run test on test trigger class

error- System.NullPointerException: Attempt to de-reference a null object
Code-To test trigger After insert.

//after insert statement. Note testcase is a listof Case. 
for(case c:testcase)
      {  AssignmentRule AR = new AssignmentRule();
        AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
        System.debug('Assignment rule id' + c.getOption().assignmentRuleHeader.assignmentRuleId);//c
        System.assertEquals(c.getOption().assignmentRuleHeader.assignmentRuleId,AR.id);//c
     
      }
Rachit Kumar JainRachit Kumar Jain
error at System.debug('Assignment rule id' + c.getOption().assignmentRuleHeader.assignmentRuleId)
asish1989asish1989
Hi

I think you are getting this error in a test class. NullPointerException means something is becoming null.

So in your code assignment rule is not there for case. First create a assignment rule in the test class before query otherwise add this attribute (seeAllData = true) in your test class so that it will access assignment rule created in your org for Case .  Mkae sure AssignmentRule is there in your org.

IsTest(seeAllData = true)

If this is what you where looking for then please mark it as a solution for others benefits.

Thank You
Sagar PareekSagar Pareek
Hi Rachit,

This is because there is no assignment rule Id assigned to your case.