You need to sign in to do that
Don't have an account?
NullPointer Exception Problem
I've got a situation where I want to display competitors on a Visualforce page -- if there are any.
My competitors are stored in Account and there's an object called Competitor__c which links the Account to itself many to many.
On my visualforce page, I've got two datatables that render the lists of competitors as per getIncumbents and getNonIncumbents.
public list<Competitor__c> getIncumbents() { return getCompetitors(account.Id, true); } public list<Competitor__c> getNonIncumbents() { return getCompetitors(account.Id, false); } public list<Competitor__c> getCompetitors(Id accId, boolean incumbent) { // pass true for incumbents and false for non-incumbents list<Competitor__c> comps = new list<Competitor__c>(); comps = [Select Opportunity__r.StageName, Opportunity__r.Name, Opportunity__c, Notes__c, LastModifiedDate, LastModifiedById, Incumbent__c, Incumbent_Value__c, Incumbent_Renewal_Date__c, Competitor__r.Name, Competitor__c, Competing_Product_Area__c from Competitor__c c where Account__c = :accId and Incumbent__c = :incumbent]; if (comps.size() > 0) { return comps; } else return null; }
The situation is that it's perfectly normal for there to not be any competitors. When there are competitors it all works fine. When there aren't there are nullPointer exceptions.
What does one do when returning nothing back is AOK??
<apex:dataTable rendered="{! NOT(err)}" value="{!incumbents}" var="c" styleClass="tableClass" columnswidth="200px,200px" cellpadding="4" border="1"> <apex:column > <apex:facet name="header">Competitors (incumbent)</apex:facet> <apex:outputText value="{!c.Competitor__r.Name}"/> </apex:column> <apex:column > <apex:facet name="header">Notes</apex:facet> <apex:outputText value="{!c.Notes__c}"/> </apex:column> <apex:column > <apex:facet name="header">Value</apex:facet> <apex:outputField value="{!c.Incumbent_Value__c}" /> </apex:column> <apex:column > <apex:facet name="header">Renewal Date</apex:facet> <apex:outputText value="{0,date,MMM d yyyy}"> <apex:param value="{!c.Incumbent_Renewal_Date__c}" /> </apex:outputText> </apex:column> <apex:column > <apex:facet name="header">Product Area</apex:facet> <apex:outputText value="{!c.Competing_Product_Area__c}"/> </apex:column> </apex:dataTable> <p/><hr/><p/> <apex:dataTable rendered="{! NOT(err)}" value="{!nonincumbents}" var="c" styleClass="tableClass" columnswidth="200px,200px" cellpadding="4" border="1"> <apex:column > <apex:facet name="header">Competitors (non-incumbent)</apex:facet> <apex:outputText value="{!c.Competitor__r.Name}"/> </apex:column> <apex:column > <apex:facet name="header">Notes</apex:facet> <apex:outputText value="{!c.Notes__c}"/> </apex:column> <apex:column > <apex:facet name="header">Opportunity</apex:facet> <apex:outputText value="{! if(c.Opportunity__r.Name != null,c.Opportunity__r.Name, ' -- ') }"/> </apex:column> <apex:column > <apex:facet name="header">Stage</apex:facet> <apex:outputText value="{!c.Opportunity__r.StageName}"/> </apex:column> <apex:column > <apex:facet name="header">Product Area</apex:facet> <apex:outputText value="{!c.Competing_Product_Area__c}"/> </apex:column> </apex:dataTable>
There must be something simple here I'm just not doing.
THANKS!!!
try this
There are several different ways that you could approach this:
With the summer '11 release there is now dynamic visualforce where you would build your VF page in your controller and use you if statement to drive it.
Alternately, you could add a few data members to your class, query your data in the constructor and set boolean data members based on the result and use the rendered attribute to determine whether the data tables are rendered. Essentially you would have two lists as data members, populate one with the incumbants and one with the nonincumbants and then test each and set a couple of booleans accordingly.
I am sure that there are other ways to approach it, but these are just a couple that came to the top of my head. Hope they help.
My guess is your problem is that you are trying to reference that list even though you are returning null.
Instead you should have
But I believe that the SOQL query would already give an empty list, which would make this work correctly.
What you definately don't want to do is return null like you are doing though.
Update your class like this
It will surely work for you