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
CSchaezCSchaez 

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;
    }
   
}

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

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

 

Message Edited by TehNrd on 07-02-2009 11:23 AM
Message Edited by TehNrd on 07-02-2009 11:25 AM

All Answers

TehNrdTehNrd

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

 

Message Edited by TehNrd on 07-02-2009 11:23 AM
Message Edited by TehNrd on 07-02-2009 11:25 AM
This was selected as the best answer
TehNrdTehNrd

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.

 

 

CSchaezCSchaez
Thanks... I'd been trying to figure out the query deal, but couldn't get it to work. this does the trick :)