+ Start a Discussion
Paul.FoxPaul.Fox 

Attempt to de-reference a null object

I am creating a visualforce page for leads.

I have the following in my controller:

Code:
Lead lead;

public Lead getLead(){
    if(lead == null) lead = [select id, name, Persona__c from Lead where id= :ApexPages.currentPage().getParameters().get('id')];
    return lead;
}

public List<Question__c> getQuestions(){
    return [select id, Question_Value__c from Question__c where Persona__c = :lead.Persona__c];
}

 Now on the Page it shows the Question ids if I just put {!Questions}.

But as soon as I try to put the questions into a pageblock I get the error: System.NullPointerException: Attempt to de-reference a null object. The error points to the return statement in the last method above.

Here is the section of the page:

Code:
<apex:pageBlockTable value="{!Questions}" var="q">
             <apex:column value="{!q.Question_Value__c}"/>
         </apex:pageBlockTable>

 
Any idea how to put the data into a table appropriately?



Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
if nothing calls getLead before getQuestions, then lead is null, you probably want

public List getQuestions(){
return [select id, Question_Value__c from Question__c where Persona__c = :getLead().Persona__c];
}

All Answers

SuperfellSuperfell
if nothing calls getLead before getQuestions, then lead is null, you probably want

public List getQuestions(){
return [select id, Question_Value__c from Question__c where Persona__c = :getLead().Persona__c];
}
This was selected as the best answer
Paul.FoxPaul.Fox
Thanks, that worked. I don't understand why the {!Questions} variable would work and the pageBlockTable wouldn't, but at least it works now.
ziaullahkhaksarziaullahkhaksar

Please help me.... I am executing the following code but it reply to my e-mail address and shows the error "Attempt to de-reference a null object" my controller code is here.. Thank u so much in Advance.

----Controller code-----

global class EmailCardListController implements Schedulable{

global void execute(SchedulableContext SC ) {

sendMail();
}
//Did the email send successfully?
public Boolean emailSent{ get; set;}
//Create the email handler;
transient Messaging.SingleEmailMessage mailHandler = new Messaging.SingleEmailMessage();

//The recipient
// String[] emailRecipient = new String[] {'zia.s3tech@gmail.com'};
public void sendMail() {

//set the recipient
mailHandler.setToAddresses(emailRecipient );

//set the reply email address
mailHandler.setReplyTo('ziaullah_uet@yahoo.com');

//set the display name
mailHandler.setSenderDisplayName('khaksar');

//set the subject
mailHandler.setSubject('Card List');

//set the template ID
//mailHandler.setTemplateId('00X90000000wsN3');

mailHandler.setHtmlBody('This is a test e-mail from Ziaullah');

try {
Messaging.sendEmail(new Messaging.Email[] { mailHandler });
emailSent = true;

}catch(EmailException e) {
System.debug(e.getMessage());
emailSent = false;
}
}

}

------Apex Page code is here----------

<apex:page controller="EmailCardListController" action="{!sendMail}" title="Email Card List" >

<apex:outputText value="{!emailSent}" />


</apex:page>

 

 

 

Paul.FoxPaul.Fox

Can you tell us which line it says the error is on?

 

I don't see any issues except emailRecipient is used to set the recipient and the line that defines emailRecipient is commented out.

ziaullahkhaksarziaullahkhaksar

sorry ... this is commented here by misktake ... its ok in actual code ...

thank u for giving time

 

devendra dhakadevendra dhaka

 

List<Account> accountList=[select Id from Account where Name=:lead.Company];
     Account account;
     Id id='01qd0000000YJAK';
     
     
     if((accountList == null)|| (accountList!=null && accountList.size()<=0))
     {
     account=new Account(Name=lead.Company);
     insert account;
     //id=account.id;
     }    
     else
      {
       system.debug(accountList.get(0).id+'hello  ');
       account=accountList.get(0);                                                                      // 'm getting this error here
       
       if(contactDuplication)
       contactList.get(0).AccountId=account.Id;
       
      }

 

 

 

Why 'm getting this erro  ??

 

then i tried to avoid using account variable, but still getting the same ??

 

modified code is posted below .

 

 

 

 

 

trigger LeadConvert on Lead (after insert) {

if(trigger.isInsert){

for(Lead lead : trigger.new)
  {
    Boolean contactDuplication;
    List<Contact> contactList=[select Id from Contact where Name=:lead.Name and Email=:lead.Email ];
     if(contactList==null||(contactList!=Null&&contactList.size()<=0))
      
         System.debug('working');
      else
        contactDuplication =true;
      
         
    Database.LeadConvert lc= new Database.LeadConvert();
    
    
    List<Account> accountList=[select Id from Account where Name=:lead.Company];
     Account account;   
     
     if((accountList == null)|| (accountList!=null && accountList.size()<=0))    // 'm getting attempt to de-refernce null object here
     {
     account=new Account(Name=lead.Company);
     insert account;
     lc.setAccountId(account.id);
     }    
     else
      {
      
       
       if(contactDuplication)
       contactList.get(0).AccountId=accountList.get(0).id;
       
       lc.setAccountId(account.id);
       
      }
  

Paul.FoxPaul.Fox

If AccountList is null, then you cannot do AccountList.size()

 

You can do all of this with three lines of code though

 

Account account = [select Id from Account where Name = :lead.Company limit 1]; // Note that you may want some sort by criteria

if(account==null) {account = new Account(Name = lead.Company);

insert account; }

TeachMeTeachMe
Thanks was trying to read standard tab and this helped me .Thanks SF for the solution.
Rafal Galazkiewicz 4Rafal Galazkiewicz 4
I'm running int the same error - attempt to de-reference a null object.
My scenario - VFP published as force.com site with a class to save a record first from entered fields and add attachmnet there as well if user attached that:

public class savemcd {
    public savemcd(ApexPages.StandardController controller)
    {
    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }

    Public Misconduct__c mymscd;
    Public Misconduct__c getmymscd()
    {
        mymscd = new Misconduct__c();
        return mymscd;
    }
   
  
    Public Pagereference Savedoc()
    {
    if (mymscd.name != null){
       Misconduct__c mcd = new Misconduct__c(Submitter__c=mymscd.Submitter__c, Phone_number__c=mymscd.Phone_number__c, Email__c=mymscd.Email__c, Description__c=mymscd.Description__c );
       insert mcd;
       string result = mcd.id;
           
           if (myfile.name != null)
           {
           Attachment a = new Attachment(parentId = result, name=myfile.name, body = myfile.body);
           /* insert the attachment */
           insert a;
           }
        }
        return NULL;
    }  
}


What do I do wrong?