0

I am unable to pass date values to constructor :Attempt to deference null object: Vf page:

 <apex:pageBlockSectionItem > 
     <apex:outputLabel value="From Date" />
     <apex:actionRegion > 
        **<apex:inputField value="{!opp.From_Date__c}">** 
            <apex:actionSupport event="onchange"
                reRender="ajax" />
          </apex:inputField> 
     </apex:actionRegion>
 </apex:pageBlockSectionItem>

Controller:

 public Opportunity opp{get;set;}//property
 public controller_VfProdPage(ApexPages.StandardController controller) 
 {
    opp = new Opportunity(); 
    startMonth =  opp.From_Date__c.month();
    numMonth= new list<integer>();
    Integer monthBtwn = opp.From_Date__c.monthsBetween(opp.To_Date__c);    
}
  • From_Date__c is coming null – Salesforce Learner Dec 7 '15 at 17:01
  • when your actionSupport executes, the values in the actionRegion are sent to the controller. Your constructor doesn't get called again. What is in the rerendered id=ajax? Do you reference {!opp.from_date__c}` within id=ajax ? – cropredy Dec 8 '15 at 1:47
  • @SalesforceLearner I think there are some basic coding rules that you may not understand. A constructor is evaluated when the page is loaded, and is not ever evaluated again. If you want to do something with the field value, you will need to have separate methods. Please look at the solution provided below by Tushar Sharma and see what the extra method allows. – DavidSchach Jan 22 '18 at 0:40
1
<apex:pageBlockSectionItem > <apex:outputLabel value="From Date" />
     <apex:actionRegion > **<apex:inputField value="{!opp.From_Date__c}">** 
      <apex:actionSupport event="onchange" action="{!priValue}" 
                            rerender="none"
      reRender="ajax" /></apex:inputField> </apex:actionRegion>
     </apex:pageBlockSectionItem> 

Controller:

    public controller_VfProdPage(ApexPages.StandardController controller) 
         {
         opp = new Opportunity(); 
        startMonth =  opp.From_Date__c.month()!=null?opp.From_Date__c.month():Date.today();  //add default value
  numMonth= new list<integer>();
  Integer monthBtwn = opp.From_Date__c!=null && opp.To_Date__c!=null?opp.From_Date__c.monthsBetween(opp.To_Date__c): 0;

        }

    public void priValue() {
         system.debug('----------------->'+opp.From_Date__c);
       startMonth =  opp.From_Date__c.month();
       numMonth= new list<integer>();
       Integer monthBtwn = opp.From_Date__c.monthsBetween(opp.To_Date__c);
    }

in your constructor opp.From_Date__c is null and you are trying to use it. First give it any value. Same with the opp.From_Date__c.monthsBetween(opp.To_Date__c); validate that these are not null.

  • I know how to pass value dynamically to controller..I have created two input field in vf page. – Salesforce Learner Dec 7 '15 at 17:17
  • you don't need to do anything extra. But before using these field in constructor validate that they are not null. Otherwise perform some action using button and in your button debug these field and you will get your value there. – Tushar Sharma Dec 7 '15 at 17:19
  • Not able to resolve – Salesforce Learner Dec 7 '15 at 17:23
  • @SalesforceLearner Please check my update answer. – Tushar Sharma Dec 7 '15 at 17:28
  • I need to get values ..you are telling me check condition whether it is null or not....it's coming null after giving input on vf page ..how to pass values to controller that is my question – Salesforce Learner Dec 7 '15 at 17:35
0

When you are calling opp.From_Date__c.month(); you are calling .month() on null so you need to first check for null value in following way

public controller_VfProdPage(ApexPages.StandardController controller) 
 {
 opp = new Opportunity(); 
 startMonth =  opp.From_Date__c.month()!=null?opp.From_Date__c.month():null;
  numMonth= new list<integer>();
  Integer monthBtwn = opp.From_Date__c!=null && opp.To_Date__c!=null?opp.From_Date__c.monthsBetween(opp.To_Date__c): 0;

}
  • Thanks!! But I need to get the user input from vf page ...I have two fields date From and To which on vf page .And it's value is coming null after giving values to it! – Salesforce Learner Dec 7 '15 at 17:31
  • constructor is called only when page load first time so you can't get updated value inside constructor. the only way is call a controller side function described by Tushar – Himanshu Dec 7 '15 at 17:35
  • 1
    Still it is showing null values – Salesforce Learner Dec 7 '15 at 17:58

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.