You need to sign in to do that
Don't have an account?

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...
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...
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.
All Answers
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.
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];
}