0

New to apex and visualforce and tried modifying Jeff Douglas' dynamic search solution for a custom object Materials. I am getting an error when I try to search (Attempt to de-reference a null object; Error is in expression '{!runSearch}' in page materialssearchpage: Class.MaterialsSearchController.runSearch: line 61, column 1)

Here is the apex class:

public with sharing class MaterialsSearchController {

// the soql without the order and limit
private String soql {get;set;}
// the collection of contacts to display
public List<Materials__c> materials {get;set;}

// the current sort direction. defaults to asc
public String sortDir {
  get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
  set;
}

// the current field to sort by. defaults to last name
public String sortField {
  get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
  set;
}

// format the soql for display on the visualforce page
public String debugSoql {
  get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
  set;
}

// init the controller and display some sample data when the page loads
public MaterialsSearchController() {
  soql = 'select name, product__c, type__c from materials__c where active__c>0';
  runQuery();
}

// toggles the sorting of query from asc<-->desc
public void toggleSort() {
  // simply toggle the direction
  sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
  // run the query again
  runQuery();
}

// runs the actual query
public void runQuery() {

 try {
    materials = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
  } catch (Exception e) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
  }

}

// runs the search with parameters passed via Javascript
public PageReference runSearch() {

  String Name = Apexpages.currentPage().getParameters().get('name');
  String Product = Apexpages.currentPage().getParameters().get('product__c');
  String Type = Apexpages.currentPage().getParameters().get('type__c');

  soql = 'name, product__c, type__c from materials__c where active>0';
  if (!Name.equals(''))
    soql += ' and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
  if (!Product.equals(''))
    soql += ' and product__c LIKE \''+String.escapeSingleQuotes(Product)+'%\'';
  if (!Type.equals(''))
    soql += ' and type__c LIKE \''+String.escapeSingleQuotes(Type)+'%\''; 


  // run the query again
  runQuery();

  return null;
}

// use apex describe to build the picklist values
}

Here is the Visualforce:

<apex:page controller="MaterialsSearchController" sidebar="false">

<apex:form >
<apex:pageMessages id="errors" />

<apex:pageBlock title="Choose Materials" mode="edit">

<table width="100%" border="0">
  <tr> 
  <td width="200" valign="top">

  <apex:pageBlock title="Parameters" mode="edit" id="criteria">

  <script type="text/javascript">
  function doSearch() {
    searchServer(
      document.getElementById("Name").value,
      document.getElementById("Product").value,
      document.getElementById("Type").value
      );
  }
  </script>

  <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
      <apex:param name="Name" value="" />
      <apex:param name="Product" value="" />
      <apex:param name="Type" value="" />
  </apex:actionFunction>

  <table cellpadding="2" cellspacing="2">
  <tr>
    <td style="font-weight:bold;">Name<br/>
    <input type="text" id="Name" onkeyup="doSearch();"/>
    </td>
  </tr>
  <tr>
    <td style="font-weight:bold;">Product<br/>
    <input type="text" id="Product" onkeyup="doSearch();"/>
    </td>
  </tr>
  <tr>
    <td style="font-weight:bold;">Type<br/>
    <input type="text" id="Type" onkeyup="doSearch();"/>
    </td>
  </tr>
  </table>

  </apex:pageBlock>

</td>
<td valign="top">

<apex:pageBlock mode="edit" id="results">

    <apex:pageBlockTable value="{!materials}" var="materialsobj">

        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Name" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!materialsobj.Name}"/>
        </apex:column>

        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Product" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Product__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!materialsobj.Product__c}"/>
        </apex:column>

        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Type" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Type__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!materialsobj.Type__c}"/>
        </apex:column>

    </apex:pageBlockTable>

</apex:pageBlock>

</td>


1

Syntactically the only thing I see in your runSearch() method what looks capable of raising that null-pointer exception is

if(!Name.equals(''))

You might write it like

if(Name != '')
1

Your Controller looks fine. But problem is in your visualforce page. Use API name for Name, Product and Type in function doSearch(), actionFunction and in table. Your Problem will be solved.

<apex:page controller="MaterialsSearchController" sidebar="false">

<apex:form >
<apex:pageMessages id="errors" />

<apex:pageBlock title="Choose Materials" mode="edit">

<table width="100%" border="0">
  <tr> 
  <td width="200" valign="top">

  <apex:pageBlock title="Parameters" mode="edit" id="criteria">

  <script type="text/javascript">
  function doSearch() {
    searchServer(
      document.getElementById("Name").value,
      document.getElementById("Product__c").value,
      document.getElementById("Type__c").value
      );
  }
  </script>

  <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
      <apex:param name="Name__c" value="" />
      <apex:param name="Product__c" value="" />
      <apex:param name="Type__c" value="" />
  </apex:actionFunction>

  <table cellpadding="2" cellspacing="2">
  <tr>
    <td style="font-weight:bold;">Name<br/>
    <input type="text" id="Name" onkeyup="doSearch();"/>
    </td>
  </tr>
  <tr>
    <td style="font-weight:bold;">Product<br/>
    <input type="text" id="Product__c" onkeyup="doSearch();"/>
    </td>
  </tr>
  <tr>
    <td style="font-weight:bold;">Type__c<br/>
    <input type="text" id="Type__c" onkeyup="doSearch();"/>
    </td>
  </tr>
  </table>

  </apex:pageBlock>

</td>
<td valign="top">

<apex:pageBlock mode="edit" id="results">

    <apex:pageBlockTable value="{!materials}" var="materialsobj">

        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Name" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!materialsobj.Name}"/>
        </apex:column>

        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Product" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Product__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!materialsobj.Product__c}"/>
        </apex:column>

        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Type" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Type__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!materialsobj.Type__c}"/>
        </apex:column>

    </apex:pageBlockTable>

</apex:pageBlock>

</td>
0

From what I can see your not handling whether the URL parameters are blank or not. I think the error stems from the Product variable:

String Product = Apexpages.currentPage().getParameters().get('product__c');

This is not returning anything (I am guessing the parameter does not exist) and the statement:

  if (!Product.equals(''))

Should be

if(Product != null && Product != '')

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.