+ Start a Discussion
Redmanx03Redmanx03 

Attempt to de-reference a null object.. Error is in expression '{!runSearch}' in page naisearch

public with sharing class CompsearchController {

  
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of Comps to display
  public List<Comp__c> Comps {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 = 'SubMarket__c'; } 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 CompsearchController(ApexPages.StandardController controller){

            
    soql = 'select Zip_Code__c, SubMarket__c, Property_Type__c, Picklist__c from Comps where Property_Type__c != null';
    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 {
      Comps = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error Please check!'));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String SubMarket = Apexpages.currentPage().getParameters().get('SubMarket__c');

 
    soql = 'select Zip_Code__c, SubMarket__c, Property_Type__c, Picklist__c from Comps ';

    if (!SubMarket.equals(''))
      soql += ' SubMarket__c Like (\''+SubMarket+'\')';
 
    // run the query again
    runQuery();
 
    return null;
  }
 
  // use apex describe to build the picklist values
  public List<String> SubMarkets {
    get {
      if (SubMarkets == null) {
 
        SubMarkets = new List<String>();
        Schema.DescribeFieldResult field = Comp__c.SubMarket__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          SubMarkets.add(f.getLabel());
 
      }
      return SubMarkets;          
    }
    set;
  }
 
}

I get the error when I run the code above... What am I doing wrong? I tried tweaking to work with a custom object.

 

 

Below is VF Page

 

<apex:page StandardController="Comp__c" extensions="CompsearchController" sidebar="false">
 
  <apex:form >

  <apex:pageMessages id="errors" />
 
  <apex:pageBlock title="Find Me A Customer!" 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("SubMarket").options[document.getElementById("SubMarket").selectedIndex].value
          );
      }
      </script>
 
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="Submarket" value="" />
      </apex:actionFunction>
 
      <table cellpadding="2" cellspacing="2">
      <tr>

        <td style="font-weight:bold;">SubMarket<br/>
          <select id="SubMarket" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!Submarkets}" var="tech">
              <option value="{!tech}">{!tech}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
 
      </apex:pageBlock>
 
    </td>
    <td valign="top">
 
    <apex:pageBlock mode="edit" id="results">
 
        <apex:pageBlockTable value="{!Comps}" var="Comp">
 
                  
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Submarket" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Submarket__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!Comp__c.SubMarket__c}"/>
            </apex:column>
 
        </apex:pageBlockTable>
 
    </apex:pageBlock>
 
    </td>
  </tr>
  </table>
 
  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />          
  </apex:pageBlock>   
 
  </apex:pageBlock>
 
  </apex:form>
 
</apex:page>

 

 

souvik9086souvik9086

soql = 'select Zip_Code__c, SubMarket__c, Property_Type__c, Picklist__c from Comps '; if (!SubMarket.equals('')) soql += ' SubMarket__c Like (\''+SubMarket+'\')';

 

You missed the "Where" clause here. Please look into it.

 

Also initialize the list variable as new in constructor

 

public with sharing class CompsearchController {


// the soql without the order and limit
private String soql {get;set;}
// the collection of Comps to display
public List<Comp__c> Comps {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 = 'SubMarket__c'; } 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 CompsearchController(ApexPages.StandardController controller){

Comps = new List<Comp__c>();
soql = 'select Zip_Code__c, SubMarket__c, Property_Type__c, Picklist__c from Comps where Property_Type__c != null';
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 {
Comps = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Error Please check!'));
}

}

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

String SubMarket = Apexpages.currentPage().getParameters().get('SubMarket__c');


soql = 'select Zip_Code__c, SubMarket__c, Property_Type__c, Picklist__c from Comps ';

if (!SubMarket.equals(''))
soql += ' Where SubMarket__c Like (\''+SubMarket+'\')';

// run the query again
runQuery();

return null;
}

// use apex describe to build the picklist values
public List<String> SubMarkets {
get {
if (SubMarkets == null) {

SubMarkets = new List<String>();
Schema.DescribeFieldResult field = Comp__c.SubMarket__c.getDescribe();

for (Schema.PicklistEntry f : field.getPicklistValues())
SubMarkets.add(f.getLabel());

}
return SubMarkets;
}
set;
}

}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Redmanx03Redmanx03

I don't get value populated on my vf page. There is data for sure..

souvik9086souvik9086

Is the error gone?

 

Redmanx03Redmanx03

Error is Gone but was due to using Comps.Submarkets vs Comps__c.Submarkets. in Page.

 

 

I still cannot render values in VF the field type is picklist. Shouldn't string work?