You need to sign in to do that
Don't have an account?
De-referencing a null object exception
my lead field incrementField__c is a number field with default value of 0. However, here when I try to increment it, It says that I am de-referencing a null object (even though the value is 0, not null). I think I may be calling the field value incorrectly, but I'm not quite sure how to get this to work properly. Any help would be greatly appreciated
*I'm pretty new to this, so I apologize if i am missing the obvious
trigger incrementStuff on Task(before insert)
{
private Task[] newTask = Trigger.new;
string subject = newTask[0].Subject;
subject.toLowerCase();
if (subject.contains ('stuff'))
newTask[0].bool__c = false;
else
{
newTask[0].bool__c=true;
string realId = newTask[0].WhoId;
realId = realId.substring(0,15); //For some reason, when I pull WhoId it attaches a few Lead
parent = new Lead(Id = realId); //random digits, so here I workaround
parent.incrementField__c = parent.incrementField__c + 1;
update parent;
}
}
Defaults are only filled in in the UI. If you create a new Leadin the code:
Lead parent = new Lead(Id = realId);
This parent object will have no default values so they will be null.
EDIT: Incorrect, see next post:
Second, I don't even think you can create a record like this in Apex:
parent = new Lead(Id = realId);
Instead you need to query the parent.
Lead parent = [select Id, Name from Lead where Id = : realId];
-Jason
All Answers
Defaults are only filled in in the UI. If you create a new Leadin the code:
Lead parent = new Lead(Id = realId);
This parent object will have no default values so they will be null.
EDIT: Incorrect, see next post:
Second, I don't even think you can create a record like this in Apex:
parent = new Lead(Id = realId);
Instead you need to query the parent.
Lead parent = [select Id, Name from Lead where Id = : realId];
-Jason
Heh, I guess you can do something like this:
Opportunity opp = new Opportunity(Id = '0065000000CLLKUAA5'); update opp;
But if you want to get any values from the object you must query them.