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

Need help understanding Null Pointer Exception

I have an inputText that is used to get a 4 digit string from the user. This string is then passed to a SOQL query as a filter. For some reason, when the 4 digit string is found in our database, the page is refreshed properly and the necessary values are displayed on the page. The problem is when the string is not found in the database and it's throwing this error:


System.NullPointerException: Attempt to de-reference a null object

 Here's the relevant section of my code:


<apex:page controller="ZipCMQuery" id="currPageId">
.activeTab {background-color: #236FBD; color:white; background-image:none}
.inactiveTab { background-color: lightgrey; color:black; background-image:none}
<apex:tabPanel switchType="client" selectedTab="IXI_DT" id="IXITabPanel"
tabClass="activeTab" inactiveTabClass="inactiveTab">
<apex:tab label="Data Table" name="IXI_DT" id="IXI_DT">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Select territory" collapsible="false">
<apex:facet name="header">Cost Center:</apex:facet>
<apex:inputText id="costCenter" title="Cost Center" value="{!myCostCenter}" maxLength="4" required="true" size="4"/><br/>
<apex:commandButton value="Search" action="{!test}" rerender="PB_JNLD_Zone" status="status7"/>
<apex:outputPanel id="out7">
<apex:actionstatus id="status7" startText="searching..."/>

<apex:pageBlock id="PB_JNLD_Zone" title="">
<apex:pageBlockSection id="PB_JNLD_Zone" title="JNLD Sales (Zone)">
<apex:outputText value="{!TotalSalesJNLDStr}"/><br/>
<apex:outputText value="{!SalesJNLDCurrCCStr}"/>


public class ZipCMQuery { private Decimal totalSalesJNLD = 0.00; private Decimal totalSalesIXI = 0.00; private Decimal salesJNLDCurrCC = 0.00; private Decimal salesIXICurrCC = 0.00; private Decimal salesJNLDCurrZS = 0.00; private Decimal salesIXICurrZS = 0.00; String myCostCenter; public String getMyCostCenter() { return myCostCenter; } public void setMyCostCenter(String s) { myCostCenter = s; } public PageReference test() { salesJNLDCurrCC = 0.00; salesIXICurrCC = 0.00; salesJNLDCurrZS = 0.00; salesIXICurrZS = 0.00; totalSalesJNLD = 0.00; totalSalesIXI = 0.00; String thisCC = getMyCostCenter(); System.debug('Value of cost center = ' + thisCC); for (Zip_Code_Metrics__c myZCM : [SELECT Cost_Center_R__c, Zip_Code__c, Primary_Mailing_City_Name__c, JNLD_R_Zone__c, JNLD_R_Sub_Zone__c, State__c, JNLD_R_Sales__c, IXI_Sales_for_JNLD_R__c From Zip_Code_Metrics__c WHERE JNLD_R_Zone__c = '1']) { if (thisCC.equals(myZCM.Cost_Center_R__c)) { salesJNLDCurrCC = salesJNLDCurrCC + myZCM.JNLD_R_Sales__c; System.debug('Value of salesJNLDCurrCC = ' + salesJNLDCurrCC); salesIXICurrCC = salesIXICurrCC + myZCM.IXI_Sales_for_JNLD_R__c; System.debug('Value of salesIXICurrCC = ' + salesIXICurrCC); } if ((myZCM.JNLD_R_Sales__c != null) && (myZCM.JNLD_R_Sales__c > 0.00) && (myZCM.JNLD_R_Sub_Zone__c.equals('B'))) { salesJNLDCurrZS = salesJNLDCurrZS + myZCM.JNLD_R_Sales__c; System.debug('Value of salesJNLDCurrZS = ' + salesJNLDCurrZS); } if ((myZCM.IXI_Sales_for_JNLD_R__c != null) && (myZCM.IXI_Sales_for_JNLD_R__c > 0.00) && (myZCM.JNLD_R_Sub_Zone__c.equals('B'))) { salesIXICurrZS = salesIXICurrZS + myZCM.IXI_Sales_for_JNLD_R__c; System.debug('Value of salesIXICurrZS = ' + salesIXICurrZS); } if ((myZCM.JNLD_R_Sales__c != null) && (myZCM.JNLD_R_Sales__c > 0.00)) totalSalesJNLD = totalSalesJNLD + myZCM.JNLD_R_Sales__c; System.debug('Value of totalSalesJNLD = ' + totalSalesJNLD); if ((myZCM.IXI_Sales_for_JNLD_R__c != null) && (myZCM.IXI_Sales_for_JNLD_R__c > 0.00)) totalSalesIXI = totalSalesIXI + myZCM.IXI_Sales_for_JNLD_R__c; System.debug('Value of salesJNLDCurrZS = ' + totalSalesIXI); } return null; } public Decimal getTotalSalesJNLDStr() { return totalSalesJNLD.setScale(2); } public Decimal getSalesJNLDCurrCCStr() { return salesJNLDCurrCC.setScale(2); } }



 When I add a debug statement, it seems that the PageReference function is not returned when the user input isn't found in our instance.


Thanks in advance.




Best Answer chosen by Admin (Salesforce Developers) 

I think you just need to put in some debug statements to see what's coming back null.  Is thisCC null?  Is myZCM ever null?



All Answers


I think you just need to put in some debug statements to see what's coming back null.  Is thisCC null?  Is myZCM ever null?



This was selected as the best answer

Thanks for the reply Jill. I've tried that and none of the variables are coming back with null. When I check the debug logs, I'm seeing that the function test (which returns a PageReference) is returning the null value when the user input matches data within our database (in my case, thisCC holds the user input). Like I said, even when the user input doesn't match the database's, thisCC is not null but the test function never returns the null value and I get the Null Pointer Exception.

Message Edited by J&A-Dev on 04-07-2009 11:54 AM

What about this part?


 Is it possible that field could be null, and the equals is throwing the NPE?


Ah, you were absolutely correct Jill. There are 2 fields that contain null values and I was getting the NPE when I was adding them. I added a couple of lines of code to check for null values and it's now working fine. Thanks Jill!