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
Natasha AliNatasha Ali 

Apex controller Attempt to de-reference a null object error!

Hi,
We've implemented an API that connects to an external database, which we can search within our Salesforce org. I'm running into an 
Attempt to de-reference a null object error:

User-added image

Here is the Visualforce Page:
<apex:page controller="EDSSearchController">
    <apex:form >
        <apex:pageBlock title="EDS Search">
            Enter search term here: <apex:inputText value="{!searchTerm}"/>
            <apex:commandButton action="{!Search}" value="Search"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And the Controller:
public class EDSSearchController {

    public String searchTerm { get; set; }
    servicesEdrsEducationGovUkV17.EmployerLookupSoap a;
    EDS_Credentials__c edsCreds;


    public EDSSearchController() {
      edsCreds = EDS_Credentials__c.getValues('Default');
        a = new servicesEdrsEducationGovUkV17.EmployerLookupSoap();
    }

    public String getSearchTerm() {
        return searchTerm;
    }

    public PageReference search() {
        a.ByFreeText(searchTerm, false,'O','',edsCreds.API_Key__c);
        return null;
    }
}

How would I resolve this error?

ANY help is MUCH appreciated!
Many Thanks,
Natasha :)
 
mohd anasmohd anas

Hi natasha,

You can setup the debug log for the current user and write some debug statements in the class like 

public PageReference search() {
        system.debug('>>>>>>searchTerm>>>>>>>>'+searchTerm);
        system.debug('>>>>>>edsCreds>>>>>>>>'+edsCreds);
        system.debug('>>>>>>API_Key__c>>>>>>>>'+edsCreds.API_Key__c);
        a.ByFreeText(searchTerm, false,'O','',edsCreds.API_Key__c);
        return null;
    }


and then check your debug log for the values of these variables. You sould also initialize you search variable with blank string in the constructor.

public EDSSearchController() {
      edsCreds = EDS_Credentials__c.getValues('Default');
        a = new servicesEdrsEducationGovUkV17.EmployerLookupSoap();
      search = '';
    }

I hope it helps.
Deepali KulshresthaDeepali Kulshrestha
Hi Natasha,
Greetings to you!

- Replace this
<apex:commandButton action="{!Search}" value="Search"/>

- From this
<apex:commandButton action="{!search}" value="Search"/>

    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Natasha AliNatasha Ali
Hi Deepali,
Thanks for your response.I tried it but I'm still getting the following error:
User-added image

Thanks!