You need to sign in to do that
Don't have an account?
Attempt to de-reference a null object on visualforce page
Hello,
I'm using a custom controller on a visualforce page to access a list of records which are unrelated. I get the de-reference null error on the visualforce page when I land on it. I am comparing a multiselect picklist field called protocols__c on the account object with a string field on an object called AlertManagement__c. If they equal each other then i want to populate them to the listGrab which my visualforce getter should show on the page. This is being accessed through the customer portal.
I realise that the null error is being thrown because of the listGrab.add(al); If I take it out the error isn't thrown.
However i know that the listFlux I am iterating over is not empty. Is there something I am not declaring or am I going about this in the wrong way? Can someone help or give me some direction?
Thanks!
Here is the visualforce page part which is accessing the controller
<apex:repeat value="{!listgrab}" var="f">
<li>
<span style="font-size: 13px;"><b>{!f.Description__c}</b>
<br/><apex:outputtext value="{!f.Alert_Message__c}" escape="false"/></span>
</li>
</apex:repeat>
Here the controller
public class portalHomePageController {
public List <Contact> oContact {get;set;}
public List <AlertManagement__c> listGrab {get;set;}
public portalHomePageController(){
User u=[select id, ContactId from User where id=:userinfo.getuserid()];
oContact=[select id, Name, Account.Name,Account.Bank__c,Account.Protocol__c from Contact where id=:u.ContactId];
List <AlertManagement__c> listFlux=[select id, Description__c,Description_FR__c, Alert_Message__c,Alert_Message_FR__c,
Bank__c,Platform__c,Protocol__c from AlertManagement__c where Active__c=true];
// iterate over the list of contacts which should only be one
for(Contact oc: oContact){
// Here i am splitting out the multiselect values into a string
string[] protocols = oc.Account.Protocol__c.split(';',0);
// Here I am iterating over the list of alerts
for(AlertManagement__c al: listFlux){
if(protocols != null && protocols.size() >0){
//Now iterate over the array of multiselect values from above
for(String p: protocols){
// the value of the string from the multiselect == the value of the text string on the aler record
if(p == al.Protocol__c){
//add the alert record to the listGrab to show on the visualforce page.
listGrab.add(al);
}
}
}
}
}
}
}
Write this statement in to your controller constructer
All Answers
Write this statement in to your controller constructer
Dahveed,
You need to initialise the listGrab list before using it for the first time. This is similar to the other OOPs languages.
So,
right after your listFlux SOQl will solve your issue.
Thanks for the replies! I needed to instantiate the listGrab.