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
pixelriesepixelriese 

Attempt to de-reference a null object - Lead -

public class ldController {
    public Lead leadRecord{get;set;}
   
    public ldController(ApexPages.StandardController sc){
     this.leadRecord = (Lead) sc.getRecord();        
    }
     
	Double leadLatitude = leadRecord.Latitude;
    Double leadLongitude = leadRecord.Longitude;
    
    List<Account> acc = new List<Account>();


    public List<Account> getAccounts(){
        Acc = Database.query('SELECT Id, Name, ShippingLatitude, ShippingLongitude, Phone FROM Account WHERE RecordTypeId=:\'01258000000ATCX\' AND DISTANCE(LOCATION(' + leadLatitude + ', ' + leadLongitude + ' ), LOCATION(ShippingLatitude, ShippingLongitude),\'km\') limit 20');    
        return Acc;
    }
}
 
<apex:page standardController="Lead" extensions="ldController">
    <apex:form >
        <apex:pageBlock title="Lead Information">
            <apex:pageBlockSection title="Details" columns="1">
               
                 Name: {! leadRecord .name} <br />
                Phone: {! leadRecord .Phone} <br />
                
            <apex:commandButton action="{! save}" value="Save"/>    
            </apex:pageBlockSection>
             <apex:pageBlockSection title="Accounts" id="results" >       
            <apex:pageBlockTable value="{!accounts}" var="a" >
            <apex:column value="{!a.id}"/>
            <apex:column value="{!a.name}"/>    
            <apex:column value="{!a.phone}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>
</apex:page>


I would like display a list which shows the nearst customers. I add a Button to the lead - pagelayout. but when i click on it, it thorws an exception :

 

Visualforce Page: /apex/ShowNearLeads



caused by: System.NullPointerException: Attempt to de-reference a null object

Class.ldController.<init>: line 8, column 1

Debug Log:
38.0 APEX_CODE,FINE;APEX_PROFILING,FINE;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,FINE;WORKFLOW,INFO
12:54:50.0 (324935)|USER_INFO|[EXTERNAL]|005580000010hIq|osrc@viessmann.com.dev1|Mitteleuropäische Normalzeit|GMT+01:00
12:54:50.0 (369104)|EXECUTION_STARTED
12:54:50.0 (374755)|CODE_UNIT_STARTED|[EXTERNAL]|0666E0000004CoG|VF: /apex/ShowNearLeads
12:54:50.0 (5930161)|VF_APEX_CALL_START|[EXTERNAL]|01p6E000000Ce2l|ldController <init>
12:54:50.0 (5944369)|SYSTEM_MODE_ENTER|true
12:54:50.0 (6376576)|METHOD_ENTRY|[1]|01p6E000000Ce2l|ldController.ldController()
12:54:50.0 (6389953)|METHOD_EXIT|[1]|ldController
12:54:50.0 (6677559)|EXCEPTION_THROWN|[8]|System.NullPointerException: Attempt to de-reference a null object
12:54:50.0 (7009926)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.ldController.<init>: line 8, column 1
12:54:50.0 (7108339)|VF_APEX_CALL_END|ldController <init>
12:54:50.19 (19837942)|CUMULATIVE_PROFILING_BEGIN
12:54:50.19 (19837942)|CUMULATIVE_PROFILING|No profiling information for SOQL operations
12:54:50.19 (19837942)|CUMULATIVE_PROFILING|No profiling information for SOSL operations
12:54:50.19 (19837942)|CUMULATIVE_PROFILING|No profiling information for DML operations
12:54:50.19 (19837942)|CUMULATIVE_PROFILING|method invocations|
External entry point: public void <init>(ApexPages.StandardController): executed 1 time in 1 ms
Class.ldController.<init>: line 8, column 1: public Lead __sfdc_leadRecord(): executed 1 time in 0 ms

12:54:50.19 (19837942)|CUMULATIVE_PROFILING_END

 

Can someone help me ?

Best Answer chosen by pixelriese
pixelriesepixelriese

It solved the previous error.

But, now I get this exception:

Only variable references are allowed in dynamic SOQL/SOSL. 
An unexpected error has occurred. Your development organization has been notified.

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try to update your code like below
public class ldController {
    public Lead leadRecord{get;set;}
    Double leadLatitude ;
    Double leadLongitude ;
    public List<Account> acc  {get;set}
  
    public ldController(ApexPages.StandardController sc)
	{
		acc = new List<Account>();
		this.leadRecord = (Lead) sc.getRecord();        
		if(leadRecord.id != null)
		{
			leadRecord = [select Latitude,Longitude from lead where id =:leadRecord.id ];
			leadLatitude  = leadRecord.Latitude;
			leadLongitude = leadRecord.Longitude;
		}
    }
    


    public List<Account> getAccounts(){
        Acc = Database.query('SELECT Id, Name, ShippingLatitude, ShippingLongitude, Phone FROM Account WHERE RecordTypeId=:\'01258000000ATCX\' AND DISTANCE(LOCATION(' + leadLatitude + ', ' + leadLongitude + ' ), LOCATION(ShippingLatitude, ShippingLongitude),\'km\') limit 20');    
        return Acc;
    }
}
Let us know if this will help you
 
pixelriesepixelriese

It solved the previous error.

But, now I get this exception:

Only variable references are allowed in dynamic SOQL/SOSL. 
An unexpected error has occurred. Your development organization has been notified.

 

This was selected as the best answer
Santosh Sriram 2310Santosh Sriram 2310
@pixelriese

That error is because of the ":" you have used right after the recordTypeID parameter. Remove it and it should work like a charm.