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
akallioWileyakallioWiley 

Help with Null Pointer Exception. SOSL in VF page

I am trying to use a SOSL query where the criteria will be input from a text box on a VF page. I am running into a null pointer exception and struggling to figure out what it is. It is my first time using SOSL. So, I'm hoping the issue will be easy to spot.

 

public with sharing class mergeMaster {

	public string searchString {get; set;}
	public List<resultWrapper> wrappedResults {get; set;}
    
       
    //fired when the Search button is clicked
    public PageReference search() {
    	    	
    	List<List<SObject>> searchList = [FIND :searchString IN ALL FIELDS RETURNING CONTACT (Id, Name, Account.Name Order By Name) ];    	
    	
    	for(Contact reCon : (List<Contact>) searchList[0]) {    		
    		resultWrapper wrCon = new resultWrapper(reCon.Id, reCon.Name, reCon.Account.Name);
    		//system.assert(false,'Results: '+wrCon);
    		wrappedResults.add(wrCon);
    	}
    	
    	return null; 
    }  
  
    
    public class resultWrapper {
        public Id contactID {get; set;}
        public String contactName {get; set;}   
        public String accountName {get; set;}
        public List<SelectOption> masterORchild {get; set;}
        
        public resultWrapper(ID cID, String cName, String aName) {
            contactID = cID;
            contactName = cName;
            accountName = aName;
            this.masterORchild = new List<SelectOption>{new SelectOption('The Master','The Master'),new SelectOption('Merge','Merge')};
        }   
    }

}

 

 

<apex:page sidebar="false" controller="mergeMaster" standardstylesheets="false">
   
    <apex:sectionheader title="Merge Master" />
    
     <apex:form id="theform">
     <apex:pageBlock mode="edit" id="block">
     
     	<apex:pageBlockSection >
        	<apex:pageBlockSectionItem >            
             <apex:panelGroup >
             	<apex:inputText id="searchString" value="{!searchString}"/>
             	<apex:commandButton value="Search" action="{!search}" rerender="block" status="status"/>
             </apex:panelGroup>
        	</apex:pageBlockSectionItem>  
        </apex:pageBlockSection><br/>
        
        <apex:actionStatus id="status" startText="Searching... please wait..."/>
        <apex:pageBlockSection >  
                    
			<apex:pageBlockSection title="Search Results" id="resultsBlock" columns="1">
 			<apex:pageBlockTable value="{!wrappedResults}" var="item" rendered="{!NOT(ISNULL(wrappedResults))}">
   				
   				<apex:column value="{!item.contactName}" headerValue="Contact" width="100"/>
   				<apex:column: value="{!item.accountName}" headerValue="Account" width="200"/>          				
 			</apex:pageBlockTable>
			</apex:pageBlockSection>
        
        </apex:pageBlockSection>            
       
      </apex:pageBlock> 
      </apex:form>  

</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

You are adding your wrapper records without initializing the list here :

 

wrappedResults.add(wrCon);

 


Initialize this list first and your issue would be gone.

 

wrappedResults = new List<resultWrapper>();
        for(Contact reCon : (List<Contact>) searchList[0]) {            
            resultWrapper wrCon = new resultWrapper(reCon.Id, reCon.Name, reCon.Account.Name);
            //system.assert(false,'Results: '+wrCon);
            wrappedResults.add(wrCon);
        }

All Answers

vishal@forcevishal@force

You are adding your wrapper records without initializing the list here :

 

wrappedResults.add(wrCon);

 


Initialize this list first and your issue would be gone.

 

wrappedResults = new List<resultWrapper>();
        for(Contact reCon : (List<Contact>) searchList[0]) {            
            resultWrapper wrCon = new resultWrapper(reCon.Id, reCon.Name, reCon.Account.Name);
            //system.assert(false,'Results: '+wrCon);
            wrappedResults.add(wrCon);
        }

This was selected as the best answer
akallioWileyakallioWiley

Thanks! I know I have made this mistake before...I'm an admin, self-taught apex coder...I guess I haven't quite grasped what 'initiating' variables is all about. oh well, it works now. Thanks again. 

vishal@forcevishal@force

You'll learn it the more you code. Happy learning and glad to help :)