function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OldDeadBugOldDeadBug 

Basic VF: What am I missing????

I am building an ultra-basic VF page with what should be an ultra-basic custom controller.

 

Hereis the code:

public class AMTransfer { public Account[] EligibleAccts; public AMTransfer() { } public Account[] getEligibleAccts() { if (!EligibleAccts.isEmpty()) EligibleAccts = [SELECT id, Name FROM Account WHERE Eligible_For_Transfer__c = 'Yes' limit 1]; system.debug('EligibleAccts size is '+EligibleAccts.size()); return EligibleAccts; }

 

The VF Code:

 

<apex:page Controller="AMTransfer" sidebar="false">
    <apex:form >
       <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockTable value="{!EligibleAccts}" var="acc">
                <apex:column value="{!acc.name}"/>                               
            </apex:pageBlockTable>     
        </apex:pageBlock>
    </apex:form>
</apex:page>

Simple, right? Now I know that there is 1 Account that meets the criteria in the query. But when I run the page:

 

/apex/AMTransfer

 

I get an Attempt to de-reference a null object

error.  My debug logs say that the process was a success, and doesn't give the system.debug message, which implies that the getEligibleAccts method isn't running. So I don't know to which null object the error message is referring.

 

What am I missing?? I haven't done any VF in a while and am probably missing something obvious, but if someone can point it out, it may save me a great deal of frustration, except for what I'm going to get from not seeing the error in the first place.

 

Thanks

rubixtiousrubixtious

I think you just need to 'new up' your object:

 

public Account[] EligibleAccts = new List<Account>();

OldDeadBugOldDeadBug

Yes, that's it!! That's the frustration I was talking about.

 

Thank you!

SteveBowerSteveBower
Or...  if you didn't "new up" as the previous post pointed out, but instead changed your If statement to:

 

if(EligibleAccts == null)

 

Then you wouldn't have to allocate the empty list, and the SELECT would only fire once.  (I think it will fire every time as it is now.)

 

Best, Steve.