+ Start a Discussion

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



I have following piece of code giving error on save click. "System.NullPointerException: Attempt to de-reference a null object error"


Any one having idea please help me out.


-================================================ Control code ================================================= public with sharing class TestForecastExtension { public TestForecastExtension (ApexPages.StandardController controller) { } private List<forecast_entry__c> forecasts; private List<forecast_entry__c> fcwrapperList; public List<foreCastWrapper> getForecastRecords() { List<foreCastWrapper> fcwrapperList = new List<foreCastWrapper> (); for(Forecast_entry__c fc : [SELECT a.Customer_name__c,a.item_name__c,a.month__c,a.forecast_qty__c,a.contact__c FROM forecast_entry__c a ]) { fcwrapperList.add(new foreCastWrapper(fc)); } return fcwrapperList;} class foreCastWrapper { public forecast_entry__c fc {get; set;} public String month1 {get; set;} public String month2 {get; set;} public String month3 {get; set;} public String month4 {get; set;} public String month5 {get; set;} public String month6 {get; set;} public String month7 {get; set;} public String month8 {get; set;} public String month9 {get; set;} public String month10 {get; set;} public String month11 {get; set;} public String month12 {get; set;} public foreCastWrapper(forecast_entry__c fc) { this.fc = fc; } } public List<forecast_entry__c> getForecasts() { forecasts =[SELECT a.Customer_name__c,a.item_name__c, a.month__c,a.forecast_qty__c,a.contact__c FROM forecast_entry__c a ]; return forecasts; } Public pagereference getUpdateData(){ update fcwrapperList; return null; } Public pagereference getUpdateData1(){ update forecasts; return null; } }




====================================== Page UI ====================================== <apex:page standardController="Forecast_Entry__c" extensions="TestForecastExtension" > <apex:form > <script type="text/javascript"> function displaymessage() { alert("Testing Javascript!"); } </script> <apex:pageBlock title="Forecast Entry Form"> <apex:pagemessages /> <apex:pageblockButtons > <apex:commandButton Value="Save" action="{!getUpdateData}" /> <apex:commandButton value="Cancel" onclick="displaymessage()"/> </apex:pageblockButtons> <apex:pageblockSection > <apex:dataTable value="{!ForecastRecords}" var="fcc" styleClass="list"> <apex:column > <apex:facet name="header">Customer Name</apex:facet> <apex:inputfield value="{!fcc.fc.Customer_Name__c}" /> </apex:column> <apex:column > <apex:facet name="header">Item Name</apex:facet> <apex:inputfield value="{!fcc.fc.Item_Name__c}"/> </apex:column> <apex:column > <apex:facet name="header"> <div align="center"> Month1 </div> </apex:facet> <input maxlength="80" name="sbstr" size="3" type="text" value="{!fcc.month1}" /> </apex:column> <apex:column > <apex:facet name="header"> <div align="center"> Month2 </div> </apex:facet> <input maxlength="80" name="sbstr" size="3" type="text" value="{!fcc.month2}" /> </apex:column> <apex:column > <apex:facet name="header"> <div align="center"> Month3 </div> </apex:facet> <input maxlength="80" name="sbstr" size="3" type="text" value="{!fcc.month3}" /> </apex:column> </apex:datatable> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>








The problem is that fcwrapperlist at the class level is always null.


You have a duplicate fcwrapperlist declared inside getForecastRecords which you populate with the wrapper classes.  When getForecastRecords completes, this data is discarded.


You fcwrapperlist (which is a different type) at the class level is never initialised, and thus is null.  When you attempt to pass this to a database update call, you get a null pointer exception.


On a related note, you can't call update on wrapper classes, only sObjects.  You'll need to turn your wrapper classes back into forecast_entry__c objects before updating then,