You need to sign in to do that
Don't have an account?
System.NullPointerException: Attempt to de-reference a null object
Hi,
I have this trigger in my sandbox and during a training session for new employees, I got this error.
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.ValidateCase: line 18, column 1
This is my trigger. I colored the error line which is line 18
/*Validations:
1. Case should be closed when case has some open taks.
*/
trigger ValidateCase on Case (before Update) {
if(Trigger.isBefore && Trigger.isUpdate)
{
Map<Id,List<Task>> CaseIdtoTasksnotclosedMap = new Map<Id,List<Task>>();
List<Task> tasks;
for(Task t : [Select Id,status,whatId,Subject from Task where whatId IN :Trigger.NewMap.keyset()])//Collect all open tasks with single query.
{
tasks = CaseIdtoTasksnotclosedMap.keyset().contains(t.whatId) ? CaseIdtoTasksnotclosedMap.get(t.whatId) : new List<task>();
if(!t.status.equalsIgnorecase('Completed')) tasks.add(t);
CaseIdtoTasksnotclosedMap.put(t.whatId,tasks);
}
for(Case c : Trigger.new)
{
if(c.status != Trigger.oldmap.get(c.Id).status && c.status.equalsIgnorecase('closed') && CaseIdtoTasksnotclosedMap.get(c.Id).size() > 0)
{
String taskNames = '';
for(task t : CaseIdtoTasksnotclosedMap.get(c.Id)) taskNames += t.Subject+' '; //collect all open task names.
c.addError('you Cannot close when case has some open tasks. {'+ taskNames +'}');//add error message to case record.
c.status.addError('Case has some open tasks');//add error message to case status field.
}
}
}
}
Please let me know if you have any query.
Thanks
Atul
Actually, this code only checks if the case about to be closed has any open cases or not.
Creation of task is in other code where when we create a case and save it, a task should be automatically created.
There is no case that matches the condition which you have highlighted in red hence the null pointer exception.
Put some debug logs to confirm this. (make sure you have the debug logs on when you execute the trigger)
system.debug('Check the size of the map:'+CaseIdtoTasksnotclosedMap.size());
for(Case c : Trigger.new)
{
system.debug('old status is: ' + Trigger.oldmap.get(c.Id));
system.debug('new status is: ' + c.status);
if(c.status != Trigger.oldmap.get(c.Id).status && c.status.equalsIgnorecase('closed') && CaseIdtoTasksnotclosedMap.get(c.Id).size() > 0)
{
String taskNames = '';
for(task t : CaseIdtoTasksnotclosedMap.get(c.Id)) taskNames += t.Subject+' '; //collect all open task names.
c.addError('you Cannot close when case has some open tasks. {'+ taskNames +'}');//add error message to case record.
c.status.addError('Case has some open tasks');//add error message to case status field.
}
You can avoid the error by putting a null pointer before you get into the For loop.
First run the debug logs and post the logs here so that rectifying the code is easier.
Hi Rov,
Thanks for your effort to bail me out. I actually got it. I tried to change the code in my red line as this
if(c.status != Trigger.oldmap.get(c.Id).status && c.status.equalsIgnorecase('closed') && CaseIdtoTasksnotclosedMap.get(c.Id) != null)
Its working now. I'm not technically sure as you as I just used a trial and error method and result is I'm not getting any errors. So guess I'm good lol..
Thanks again though. Keep in touch!