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
Sachin Sharma 3886Sachin Sharma 3886 

VF Page,Attempt to de-reference a null object

Hi,

I am working on some custom Controller and VF pages, but the page is throwing error that 

System.NullPointerException: Attempt to de-reference a null object 
Class.ControllerApexsharing.<init>: line 7, column 1

My Class and VF pages are below.
Class---
public class ControllerApexsharing{
 
   public List<ApexSharing__c> SelectedApexSharing{ get; set; }
          public ControllerApexsharing(){
           for(ApexSharing__c Apexs :[select id,name, Field_1__c,Field_2__c,Account__c from ApexSharing__c]){
                system.debug('***' + Apexs.name +'***');
            SelectedApexSharing.add( Apexs);
            }
     }

}
VF page---
<apex:page controller="ControllerApexsharing">
  <apex:form >
      <apex:pageBlock >
           
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!SelectedApexSharing}" var="LoopVar">
                    <apex:column value="{!LoopVar.id}"/>
                    <apex:column value="{!LoopVar.name}"/>
                    <apex:column value="{!LoopVar.Field_1__c}"/>
                    <apex:column value="{!LoopVar.Field_2__c}"/>
                    <apex:column value="{!LoopVar.Account__c }"/>

                </apex:pageBlockTable>
            </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
 </apex:page>

Please suggest...

 
Best Answer chosen by Sachin Sharma 3886
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Luis Luciani
Hi Sachin,

The error you are getting is because you are trying to add items into the "SelectedApexSharing" list before it has been initialized.

Update your controller code as follows and it should work. Note that I am initializing "SelectedApexSharing" right before the for loop.

 
public class ControllerApexsharing
{
   public List<ApexSharing__c> SelectedApexSharing{ get; set; }
          
   public ControllerApexsharing()
   {
      SelectedApexSharing = new List<ApexSharing__c>();
      for(ApexSharing__c Apexs :[select id,name, Field_1__c,Field_2__c,Account__c from ApexSharing__c])
      {
          system.debug('***' + Apexs.name +'***');
          SelectedApexSharing.add( Apexs);
      }
   }
}

 

All Answers

Ajay Nagar 7Ajay Nagar 7
Hi Sachin,
List<ApexSharing__c> sharinglst=[select id,name, Field_1__c,Field_2__c,Account__c from ApexSharing__c];
			If(sharinglst!=null && sharinglst.size()>0){
				for(ApexSharing__c Apexs :sharinglst){
					if(Apexs!=null)
					SelectedApexSharing.add( Apexs);
				}
			}
Use this code.
 
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Luis Luciani
Hi Sachin,

The error you are getting is because you are trying to add items into the "SelectedApexSharing" list before it has been initialized.

Update your controller code as follows and it should work. Note that I am initializing "SelectedApexSharing" right before the for loop.

 
public class ControllerApexsharing
{
   public List<ApexSharing__c> SelectedApexSharing{ get; set; }
          
   public ControllerApexsharing()
   {
      SelectedApexSharing = new List<ApexSharing__c>();
      for(ApexSharing__c Apexs :[select id,name, Field_1__c,Field_2__c,Account__c from ApexSharing__c])
      {
          system.debug('***' + Apexs.name +'***');
          SelectedApexSharing.add( Apexs);
      }
   }
}

 
This was selected as the best answer
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
hi sachin,
your created handle list is selectedapexsharing and  records to get soql is list type ,we can use below code or another wise ajaynagar posted code you use it 

public ControllerApexsharing(){
          SelectedApexSharing= [select id,name, Field_1__c,Field_2__c,Account__c from ApexSharing__c];
               
            }