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
DahveedDahveed 

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);
                                
                        
                            }
                        }
                    }    
                }
        }
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Shiv ShankarShiv Shankar

Write this statement in to your controller constructer

if(listGrab == Null){
     listGrab = new List <AlertManagement__c>();
}

 

All Answers

Shiv ShankarShiv Shankar

Write this statement in to your controller constructer

if(listGrab == Null){
     listGrab = new List <AlertManagement__c>();
}

 

This was selected as the best answer
Shiv ShankarShiv Shankar
It's good if you make it first line in controller constructer.
ashishkrashishkr

Dahveed,

You need to initialise the listGrab list before using it for the first time. This is similar to the other OOPs languages.

 

So, 

 listGrab = new List <AlertManagement__c>();

 right after your listFlux SOQl will solve your issue.

 

 

Rahul SharmaRahul Sharma
If above does not resolves your issue, then write some system.debug's statement and use developer console/debug logs to find exact issue.
DahveedDahveed

Thanks for the replies! I needed to instantiate the listGrab.