1

I am new to Apex coding. Can someone please help me to resolve the issue. I am trying to automatically display the duration when from and todate is selected. For that i have developed a code in the following way.

public class simplegetset{

    public String name;
    public Date fromdate;
    public Date todate;
    public String city;
    public String area;
    public Integer duration;
    public String address;
    public servicehomes__c servhomes{get;set;}

    public simplegetset(){
        servhomes = new servicehomes__c();
    }

    // -------   Methods   ---------
    public String getaddress() { return null; }                   
    public void setaddress(String address) { this.address = address;}    

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        List<account> accounts = [select area__c,Billingcity,Billingstreet from account];
        for(account c: accounts ) {
            if(c.area__c != servhomes.area__c) {
                options.add(new SelectOption(c.Billingcity+c.billingstreet,c.Billingcity+c.billingstreet));
            }
        }
        return options;
    }        

    // !!!!!   ACTIONS    !!!!!
    public PageReference duration() {
        system.debug('duration'+duration);
        duration = fromdate.daysBetween(todate);
        return null;
    }

    public PageReference display() {
        List<account> accounts = [select area__c,Billingcity,Billingstreet from account];
        /*getItems(accounts); */
        return null;
    }


}//END class   

VF page:

<apex:page Controller="simplegetset" >
   <apex:form id="apexform" >
    <apex:pageMessages />
        <apex:pageBlock title="Servicehomes Edit">
        <apex:commandButton value="save" action="{!save}" id="thebutton"/>

        <apex:pageBlockSection columns="2">
            <apex:inputfield id="Tenenthome" value="{!servhomes.Name}"/>
            <apex:inputfield id="City" value="{!servhomes.City__c}"/>
            <apex:inputfield id="area" value="{!servhomes.Area__c}">
            <apex:actionSupport id="areaselection" event="onchange" action="{!display}" reRender="Address"/>
            </apex:inputfield>
           <apex:outputtext id="Address" value="{!servhomes.Address__c}">
           <apex:selectRadio value="{!address}">
            <apex:selectOptions value="{!items}" />
                </apex:selectRadio>
                </apex:outputtext>
            <apex:inputfield id="FromDate" value="{!servhomes.Fromdate__c}"/>
            <apex:inputfield id="ToDate" value="{!servhomes.Todate__c}">
            <apex:actionSupport event="onchange" status="status" action="{!duration}" reRender="duration"/>
            </apex:inputfield>
            <apex:outputField id="duration" value="{!servhomes.Duration__c}"/>
             </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

I have a formula field called duration in my object. But when saving the record like this i am getting the System.NullPointerException: Attempt to de-reference a null object error. If i am commenting in pagerefernce duration section and saving the record the value is saved. I thaught the value is saving is the value that comes form the formula. Can you please help me how and where to modify the code .

  • 2
    Could you please tell us the line of the error? You could get this data from the debug log. It will be useful – Martin Borthiry Jan 29 '14 at 10:07
  • @user5558, I cleaned up your class to make it a lot easier to understand. Try setting up your class by putting your variables first, then your constructor, then methods. Keeping neat code will save you an eternity in debugging – Scott Pelak Jan 29 '14 at 18:18
1

Assuming this line existed there before you started to troubleshoot the issue.

This line system.debug('duration'+duration);

You are referencing the duration value but it has no value at that point.

| improve this answer | |
  • This would not give error System.NullPointerException: Attempt to de-reference a null object error – Sonal Jan 29 '14 at 10:58
0

When you call duration = fromdate.daysBetween(todate);, duration is not yet declared! Declare the variable first, e.g. Pagereference duration;.

| improve this answer | |
0

This isn't a solution per-se, but it's a really good idea to initialize all of your variables in your constructor. Guaranteeing all of your variables are initialized in the constructor lets you know the issue cannot be there -- the variable must be set null from within a method . . .

public class simplegetset{

    public String name;
    public Date fromdate;
    public Date todate;
    public String city;
    public String area;
    public Integer duration;
    public String address;
    public servicehomes__c servhomes{get;set;}

    public simplegetset(){
         this.name = '';
         this.fromDate = date.today();
         this.toDate = date.today();
         this.city = '';
         this.area = '';
         this.duration = 0;
         this.address = ''; 
         this.servhomes = new servicehomes__c();
    }//END init()

    // -------   Methods   ---------
    public String getaddress() { return null; }                   
    public void setaddress(String address) { this.address = address;}    

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        List<account> accounts = [select area__c,Billingcity,Billingstreet from account];
        for(account c: accounts ) {
            if(c.area__c != servhomes.area__c) {
                options.add(new SelectOption(c.Billingcity+c.billingstreet,c.Billingcity+c.billingstreet));
            }
        }
        return options;
    }        

    // !!!!!   ACTIONS    !!!!!
    public PageReference duration() {
        system.debug('duration'+duration);
        duration = fromdate.daysBetween(todate);
        return null;
    }

    public PageReference display() {
        List<account> accounts = [select area__c,Billingcity,Billingstreet from account];
        /*getItems(accounts); */
        return null;
    }


}//END class    
| improve this answer | |

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.