0

I have a VF page in which there are 2 Date field. But when I click on Show button those are not passing the value in show method.

<apex:page controller="SelectTimePeriod_VFController">
<apex:form >
<apex:pageBlock title="Select Start and End Date">
    <apex:pageBlockSection >
        <apex:inputField value="{!projectDetails.Name}"/>
        <apex:inputField value="{!projectDetails.Project_End_Date__c}"/>
        <apex:commandButton action="{!showData}" value="Show" />
    </apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Project and Resource Details">
</apex:pageBlock>
</apex:form>

public with sharing class SelectTimePeriod_VFController {

    public static Project__c projectDetails {get; set;}
    public static Boolean isDateSelected {get; set;} 
    public static String Check{get; set;}  

    public SelectTimePeriod_VFController() {
        projectDetails = new Project__c();
        isDateSelected=false;
    }

    public PageReference ShowData(){
        System.debug('$$$$Test: '+ Check);
        System.debug('$$$$Test: '+ projectDetails.Name);
        System.debug('$$$$Test: '+ projectDetails.Project_End_Date__c);
        isDateSelected=true;

        return null;
    }
}

The error I am getting:

System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!showData}' in component in page selecttimeperiod_vfpage: Class.SelectTimePeriod_VFController.ShowData: line 14,column 1 Class.SelectTimePeriod_VFController.ShowData: line 14,column 1

0
2

To fix, remove the static from the properties e.g.:

public static Project__c projectDetails {get; set;}

becomes:

public Project__c projectDetails {get; set;}

as static fields are not included in the view state mechanism of Visualforce that ensures controller values are populated when a request is made back to the server.

See e.g. Apex Properties.

0
0

As Keith stated you will have to remove static and change to sObject.Please find the examples below to get values from vf to controller:

VF Page:

<apex:page>
<apex:form>
<apex:inputfield value="{!co.CustomField__c}"/>
</apex:form>
</apex:page>

Controller

public with MyController {
/*Controller code sets this custom object reference
When the page is first displayed the CustomField__c value comes from here
When the form is submitted, the CustomField__c value is automatically 
updated*/

public CustomObject__c co {get; set;}

// An action initiated from the Visualforce
public PageReference save() {

system.debug('@@@@@@@@@@' +co.Custom_field__c);
}
}

Example 2:

<apex:page controller="aaPage52">
    <apex:form id="RID">
        <apex:commandButton value="click" action="{!TestX}" rerender="RID">
            <apex:param name="X" value="{!X + 1}" />
        </apex:commandButton>
        <apex:outputText value="{!X}"/><br/>

        <apex:commandButton value="click" action="{!TestX2}" rerender="RID">
            <apex:param assignTo="{!X2}" value="{!X2 + 1}" />
        </apex:commandButton>
        <apex:outputText value="{!X2}"/><br/>

    </apex:form>
</apex:page>

Controller

public class aaPage52 {
    public Integer X { get; set; }
    public Integer X2 { get; set; }

    public aaPage52() {
        X = 0;
        X2 = 0;
    }
    public PageReference TestX() {
        String strX = ApexPages.currentPage().getParameters().get('X');
        if (strX != null) {
            this.X = Integer.ValueOf(strX);
        }
        return null;
    }
    public PageReference TestX2() {
        return null;
    }
}

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.