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

NULL pointer exception issue in custom object
Hello
I have a custom object fan which has three fields, email , firstname and lastname , email field being mandatory.
first time when page loads, I append an ID known as encryptedfanID to the browser URL , thereby it loads the respective values into the fields.
so this part is working fine
suppose if I do not append any ID to the URL and hit the save button without entering any valies then it throws error " ateempt to deference a null object" error.
a piece of code explaining the above is provided.
thanks
JohnD
I have a custom object fan which has three fields, email , firstname and lastname , email field being mandatory.
first time when page loads, I append an ID known as encryptedfanID to the browser URL , thereby it loads the respective values into the fields.
so this part is working fine
suppose if I do not append any ID to the URL and hit the save button without entering any valies then it throws error " ateempt to deference a null object" error.
a piece of code explaining the above is provided.
public class PrefCenterCTRL{ //Profile Tab public String fanLastName { get; set; } public String fanFirstName { get; set; } public String fanEmail { get; set; } public String encryptedfanID{get;set;} public fan__c fan{get;set;} public PrefCenterCTRL() { try { encryptedfanID=ApexPages.currentpage().getparameters().get('id'); system.debug('@@@'+encryptedfanID); if(String.isBlank(ApexPages.currentPage().getParameters().get('id'))) { ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Id.'); ApexPages.addMessage(myMsg); return; } else { fetchfanvalues(); } } catch(Exception e){ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()); ApexPages.addMessage(myMsg); return; } } public void fetchfanvalues() { try { fan=[SELECT id, Email__c,First_Name__c,Last_Name__c, FROM fan__c WHERE Encrypted_ID__c=:encryptedfanID]; if(!string.isBlank(fan.Email__c)) { fan_email=fan.Email__c; } if(!string.isBlank(fan.First_Name__c)) { fanFirstName=fan.First_Name__c; } if(!string.isBlank(fan.Last_Name__c)) { fanLastName=fan.Last_Name__c; } } public void SaveValues() { if(!string.isBlank(fan_email)) { fan.Email__c=fan_email; } if(!string.isBlank(fanMobile)) { fan.Mobile_Phone__c=fanMobile; } if(!string.isBlank(fanFirstName)) { fan.First_Name__c=fanFirstName; } public PageReference btn_profile_saveChanges() { SaveValues(); return null; } } <apex:page controller="PrefCenterCTRL" docType="html-5.0" showHeader="false" > <apex:form> <apex:inputText value="{!fanEmail}" id="email_val"/> <apex:inputText value="{!fanfirstname}" id="firstname"/> <apex:inputText value="{!fanLastName}" id="lastname"/> <apex:commandButton value="SAVE CHANGES" action="{!btn_profile_saveChanges}" /> <apex:form> </apex:page>I hope I am pretty clear, I request the forum membets to kndly help me out.
thanks
JohnD
That is because you are not creating instance for fan__C.
Try this code
Let us know if it helps.
Mark it as best answer if it works.
Thanks.
All Answers
That is because you are not creating instance for fan__C.
Try this code
Let us know if it helps.
Mark it as best answer if it works.
Thanks.
That worked. stupid of me to miss out on that.
thanks very much.
John