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);    
}
3
  • From_Date__c is coming null Dec 7, 2015 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, 2015 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. Jan 22, 2018 at 0:40

2 Answers 2

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.

7
  • I know how to pass value dynamically to controller..I have created two input field in vf page. Dec 7, 2015 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. Dec 7, 2015 at 17:19
  • Not able to resolve Dec 7, 2015 at 17:23
  • @SalesforceLearner Please check my update answer. Dec 7, 2015 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 Dec 7, 2015 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;

}
3
  • 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! Dec 7, 2015 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, 2015 at 17:35
  • 1
    Still it is showing null values Dec 7, 2015 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .