+ Start a Discussion
Baird_SBaird_S 

getters and setters not working? Should be EASY??

This ought to be really simple.  But I've beaten my head against it for a while, so humbly request your guidance.

 

I'm creating a VF page to allow users to enter both account and contact data together.  I've stripped it down to the simplest version just to debug it.

 

The user enters the Account.name, Contact.firstname and Contact.lastname and clicks submit.

 

<apex:page Controller="enterAccountContact3" showheader="false">
<apex:form >
<h2>Account</h2><br/>
Account Name <apex:inputfield value="{!acct.name}"/><br/>
<h2>Contact</h2>
First name" <apex:inputfield value="{!ctct.FirstName}" required="true" /><br/>
Last name  <apex:inputfield value="{!ctct.LastName}" required="true" /><br/>

<apex:commandButton action="{!submit}" value="Submit" immediate="true" />
</apex:form>
</apex:page>

When the user clicks "submit," the controller is supposed to insert the acct and then ctct (with ctct.accountid=acct.id).

 

public class enterAccountContact3 {

    public Contact ctct {get; set;}
    public Account acct {get; set;}

    public enterAccountContact3() {
    ctct = new Contact();
    acct = new Account();}

public pageReference submit(){
    system.debug('At the beginning of submit(), acct is '+acct.name+' and contact is '+ctct.firstname+
     ' '+ctct.lastname);
    insert acct;
    Ctct.accountid = acct.id;
    insert ctct;
    PageReference done = Page.enteredAccountContactPage;
    done.setRedirect(false);
    return done;
}

But the insert actually never begins.

 

When submit() is invoked, I get: Attempt to de-reference a null object, and the line number references the acct.name.  I've tried to solve this various ways, with an explicit getter, etc., but am never able to get the values entered on the VF page to be available to the controller.  So even at the beginning of the submit() method, before any other validation rules or anything has been invoked, the acct.name is still null.  

 

What am I missing here?

 

Many thanks in advance for your help,

Baird

aballardaballard

You haven't created any contact or account objects so they are, indeed, null. 

 

To do things this way, you need to create objects in the constructor to which you then bind the inputField tags.  Then the values you enter will be stored in those objects and you can insert the objects. 

 

Seems like you could use standard controller for most or all of this.

TejTej
<apex:page Controller="enterAccountContact3" showheader="false">
<apex:form >
<h2>Account</h2><br/>
Account Name <apex:inputText value="{!aname}"/><br/>
<h2>Contact</h2><br/>
First name" <apex:inputText value="{!CFName}" /><br/>
Last name  <apex:inputText value="{!ClName}" /><br/>

<apex:commandButton action="{!submit}" value="Submit"  />
</apex:form>
</apex:page>
public class enterAccountContact3 {


    public String aname {get; set;}
    public String CFName {get; set;}
    public String ClName {get; set;}

    public enterAccountContact3() {
    }


    public pageReference submit(){

        system.debug('Inside the submit******************');
        system.debug('Whats coming into aname*****************'+aname);
        system.debug('Whats coming into  CFName*****************'+CFName );
        system.debug('Whats coming into ClName *****************'+ClName );
        
        Account a = new Account();
        a.Name = aname ;
        insert a;
        
        Contact c = new Contact();    
        c.accountid = a.id;
        c.FirstName = CFName ;
        c.LastName = ClName ; 
        insert c;
        
        PageReference done = Page.AccountContactCreate;
        done.setRedirect(false);
        return done;
    }
}

 

 Try this,

 

BritishBoyinDCBritishBoyinDC

Looks like that was an oversight in the stripping down...they are there now...

 

But the reason this is failing even with the objects being created is the 'immediate = true' on the button - that seems to override the setters, so the controller doesn't know what the values are when it saves the records...

 

The immediate is there I think to allow the user to not have to set the account name in all cases, and instead set it to the something else in the controller, but if account name is left blank, it won't let you submit the page...

 

So the easiest solution is to instead use a string variable to store the account name value, and then handle the logic in the controller of whether to use the entered string or something else... 

aballardaballard

Right.  immediate means go directly to the action.   Intended for things like cancel buttons.   It bypasses validations and setting values from the ui into the controller.  (Since they don't get validated, it is not appropriate to set them since they might be invalid for the destination...)

 

 

Baird_SBaird_S

You guys are awesome.  An initial try of BritishBoyinDC's code runs fine; I will get back to you and identify the solution when I'm done.