0

Below is my controller class code

public Account acctObj {
    get;
    set {
        new Account();
     }
}
public boolean checkMessageFlag {
    get;
    set {
        checkMessageFlag = true;
    }
}
public PageReference updateViewStateForCreditCheckMessage() {
    if(acctObj.Decline_Credit_Check__c == true) {
        checkMessageFlag = false;
    }
    return null;
 }

And below is the markup for my Visualforce Page

<apex:outputLabel id="creditCheckMessageId" value="*Credit Check has not been performed"
                            style="font-size: inherit;color:red;" rendered="{!checkMessageFlag}" ></apex:outputLabel>
                            <br></br>
                    &nbsp;&nbsp;
                    <apex:outputLabel ><apex:inputField value="{!acctObj.Decline_Credit_Check__c}">
                     <apex:actionSupport event="onchange"
                                                action="{!updateViewStateForCreditCheckMessage}"
                                                reRender="creditCheckMessageId"/>
                    </apex:inputField>&nbsp;&nbsp;Decline Credit Check </apex:outputLabel>

I am trying to remove the text when the check box is checked. but whenever I try to check the box i get an error saying "Attempt to dereference a null object". Any hep in this ?

2

You really must read up on and understand Apex Properties. It's too long to quote at length, but here's the basic idea:

public class BasicProperty {
   public integer prop {
      get { return prop; }
      set { prop = value; }
   }
}

The problem with your code is you never actually instantiate your acctObj property. The correct way would be to either put it in the constructor (preferred) or use the getter, since you're never calling the setter anywhere.

Constructor

public Account acctObj { get; private set; }
public MyController(/*any parameters*/)
{
    accctObj = new Account();
}

Getter

public Account acctObj
{
    get
    {
        if (acctObj == null) acctObj = new Account();

        // you need to perform an assignment somewhere
        // as in the above line
        // this pattern is called Lazy Loading

        return acctObj;
    }
    private set;
}
  • It does not gives the error but it also does not shows the output label when I uncheck the checkbox, which was the functionality I was trying to achieve to show the text based on checkbox value – user5129806 Jan 6 '17 at 3:13
  • Well, that sounds like a separate question, as this is the core issue you had with your code. If your understanding is at this level, you should strongly consider working through all the related Trailhead modules you can find. – Adrian Larson Jan 6 '17 at 3:14
  • @AdrianLarson Did you review their previous questions...? – sfdcfox Jan 6 '17 at 3:15
  • No I didn't know they had been asking a series. – Adrian Larson Jan 6 '17 at 3:16

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.