You need to sign in to do that
Don't have an account?
![krishna casukhela 7 krishna casukhela 7](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
deference NULL 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 values then it throws error " ateempt to deference a null object" error.
Thanks
Krishna
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 values then it throws error " ateempt to deference a null object" error.
public class PrefCenterCTRL{ 02 03 //Profile Tab 04 public String fanLastName { get; set; } 05 public String fanFirstName { get; set; } 06 public String fanEmail { get; set; } 07 public String encryptedfanID{get;set;} 08 public fan__c fan{get;set;} 09 10 public PrefCenterCTRL() 11 { 12 try 13 { 14 encryptedfanID=ApexPages.currentpage().getparameters().get('id'); 15 system.debug('@@@'+encryptedfanID); 16 17 if(String.isBlank(ApexPages.currentPage().getParameters().get('id'))) 18 { 19 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Id.'); 20 ApexPages.addMessage(myMsg); 21 return; 22 } 23 else 24 { 25 26 fetchfanvalues(); 27 } 28 29 } 30 catch(Exception e){ 31 ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()); 32 ApexPages.addMessage(myMsg); 33 return; 34 } 35 } 36 37 public void fetchfanvalues() 38 { 39 try 40 { 41 fan=[SELECT id, Email__c,First_Name__c,Last_Name__c, 42 FROM fan__c 43 WHERE Encrypted_ID__c=:encryptedfanID]; 44 45 46 if(!string.isBlank(fan.Email__c)) 47 { 48 fan_email=fan.Email__c; 49 } 50 51 if(!string.isBlank(fan.First_Name__c)) 52 { 53 fanFirstName=fan.First_Name__c; 54 } 55 56 if(!string.isBlank(fan.Last_Name__c)) 57 { 58 fanLastName=fan.Last_Name__c; 59 } 60 } 61 62 public void SaveValues() 63 { 64 65 if(!string.isBlank(fan_email)) 66 { 67 fan.Email__c=fan_email; 68 } 69 70 if(!string.isBlank(fanMobile)) 71 { 72 fan.Mobile_Phone__c=fanMobile; 73 } 74 75 if(!string.isBlank(fanFirstName)) 76 { 77 fan.First_Name__c=fanFirstName; 78 } 79 public PageReference btn_profile_saveChanges() 80 { 81 SaveValues(); 82 return null; 83 } 84 } 85 86 <apex:page controller="PrefCenterCTRL" 87 docType="html-5.0" 88 showHeader="false" > 89 90 <apex:form> 91 92 <apex:inputText value="{!fanEmail}" id="email_val"/> 93 <apex:inputText value="{!fanfirstname}" id="firstname"/> 94 <apex:inputText value="{!fanLastName}" id="lastname"/> 95 <apex:commandButton value="SAVE CHANGES" 96 action="{!btn_profile_saveChanges}" /> 97 <apex:form> 98 </apex:page>Please help me out.
Thanks
Krishna
That is because you are not creating instance for fan__C.
Add this statement in the controller. Add another if condition befor accessing 'Fan' fields.
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.
Add this statement in the controller. Add another if condition befor accessing 'Fan' fields.
Let us know if it helps.
Mark it as best answer if it works.
Thanks.
i don't think your line no 81/41 is correct because query result is Type List and fan is only object type you declare at top
public fan__c fan{get;set;}
change it with
public List<fan__c> fan{get;set;}
and also create object of fan__c use null check before your code
Thanks
Replace
if(String.isBlank(ApexPages.currentPage().getParameters().get('id')))
by
if (ApexPages.currentPage().getParameters().get('id')!='' || ApexPages.currentPage().getParameters().get('id')!=null)
The code will work fine now :)
Generally we use String.isBlank() to check if textbox is empty or not.
I guess ,not sure but probably in your case ApexPages.currentPage().getParameters().get('id') returned null so String.isBlank() function returned true instead of returning false.That is why code crashed with null point exception.
Thanks and Regards,
Shiva RV.
ApexPages.currentPage().getParameters().get('id')!='' || ApexPages.currentPage().getParameters().get('id')!=null in this way is because , in map if key doesnt exist it returns null, that is
ApexPages.currentPage().getParameters().get('id')!=null is true if in your url you haven't written ?id=
while
ApexPages.currentPage().getParameters().get('id')!='' is true , if in your url , you have written ?id=(empty string)
Thanks and Regards,
Shiva RV