public List<sObject> sobjlst {get; set;}
public void find() {
    if(searchText != null && searchText.length() > 1){
        List<List<SObject>> results = [FIND :('*' + searchText + '*') IN ALL FIELDS RETURNING 
            Contact (Id, Name, Email, Account.Name,MailingStreet, MailingCity, MailingPostalCode, MailingState, MailingCountry,Location__Latitude__s, Location__Longitude__s) ,
            Account (Id, Name, Billingstreet, Billingcity, BillingPostalcode, Billingstate, Billingcountry, Location__Latitude__s, Location__Longitude__s) ,
            Lead (Id, Name, Email, street,city,postalcode,state,country, Location__Latitude__s, Location__Longitude__s)
        contacts = (List<Contact>)results[0];
         if(results!=null && results.isempty()){
         for(list<sObject> objectList :results ){
            sobjlst .addall(objectList);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'No matches for "' + searchText + '"'));
        } else {
    } else {
        if(sobjlst != null) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Please provide at least 2 characters for the search.'));

System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!find}' in component in page findnearby1: Class.mapController.find: line 31, column 1

Class.mapController.find: line 31, column 1

Vf page :

<apex:form id="form">
    <apex:pageBlock id="searchBlock">
        <apex:inputText value="{!searchText}" />
        <apex:commandButton value="Search" action="{!find}"/>
        <p>Examples: <a href="/apex/{!$CurrentPage.Name}?q=USA">"USA"</a>, "Singapore", "Uni", "(336) 222-7000". If it works in the global search box, it will work here.</p>
    <apex:pageBlock title="Found {!sobjlst.size} Contact(s)..." rendered="{!NOT(ISNULL(sobjlst)) && sobjlst.size > 0}" id="resultsBlock">
        <apex:pageBlockButtons location="top">
            <apex:commandButton value="Clear cached locations" title="Click if you want to set 'null' as geolocation info for all these contacts" action="{!clearGeocodedData}" />
        <apex:pageBlockTable value="{!sobjlst}" var="c" id="contacts">
            <apex:column headerValue="{!$ObjectType.Contact.fields.Name.label}">
                <apex:outputLink value="../{!c['Id']}">{!c['Name']}</apex:outputLink>
        <pre id="log"></pre>

How can I correct my code?


2 things to consider:

  1. The for loop is wrong here. sobjlst is a list of sObject and you are adding list. The code will break here.

for(list<sObject> objectList :results ){sobjlst.addall(objectList);}

  1. if(sobjlst.isEmpty()) this check is resulting into NullPointerException. Since sobjlst is not initialized or assigned any value, its null and you can't use .(dot) operator in a null variable.

As a solution, initialize your sobjlst like this before the for loop.

sobjlst = new List<sObject>();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.