You need to sign in to do that
Don't have an account?
Abhishek Kedari
Error in accessing fields in controller : System.NullPointerException: Attempt to de-reference a null object
Hi All,
I have question -
I created one object 'sampleObject' with one field 'Name' in it and visualforce page and controller code as follows
Visualforce page code :
<apex:page controller="SampleClass">
<apex:form id="RID">
<apex:pageBlock title="Sample Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Basic Information" columns="1">
<apex:inputText label="Name" value="{!sampleObj.Name__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller Code :
public class SampleClass {
public sampleObject__c sampleObj { get; set; }
public PageReference save() {
System.Debug(sampleObj.Name__c);
return null;
}
}
Now whenever I click on save button it gives me error -
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page samplepage: Class.SampleClass.save: line 6, column 1
Can anyone tell me where am I going wrong ?
Thanks,
Abhishek
I have question -
I created one object 'sampleObject' with one field 'Name' in it and visualforce page and controller code as follows
Visualforce page code :
<apex:page controller="SampleClass">
<apex:form id="RID">
<apex:pageBlock title="Sample Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Basic Information" columns="1">
<apex:inputText label="Name" value="{!sampleObj.Name__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller Code :
public class SampleClass {
public sampleObject__c sampleObj { get; set; }
public PageReference save() {
System.Debug(sampleObj.Name__c);
return null;
}
}
Now whenever I click on save button it gives me error -
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!save}' in component <apex:commandButton> in page samplepage: Class.SampleClass.save: line 6, column 1
Can anyone tell me where am I going wrong ?
Thanks,
Abhishek
you need to initialise the sampleObj variable...
You can do this in a constructor....
All Answers
you need to initialise the sampleObj variable...
You can do this in a constructor....