You need to sign in to do that
Don't have an account?
GC_00001
Getting substring of a field produces error message Attempt to de-reference a null object. What have I missed?
Not experienced in apex code so would appreciate some help.
Within a controller I am trying to retrieve the first 3 characters of a field to either store or just query on direct.
I have the following two fields defined:
public String orderHeaderReason {get; set;}
public String orderHeaderReasonFirst3 {get; set;}
and then want to use them as follows:
// Save the reason specified on the OrderHeader - this works OK
orderHeaderReason = orderHeader.Reason_for_Order_FOC__c;
// Save the first 3 characters of the same field - this fails
orderHeaderReasonFirst3 = orderHeader.Reason_for_Order_FOC__c.substring(0,2);
//This also fails
orderHeaderReasonFirst3= orderHeaderReason.substring(0,2);
I am assuming that I have not correctly defined the fields orderHeaderReason and orderHeaderReasonFirst3 but cannot see what I should be doing - I need to instantiate them in some way?
Thanks
Within a controller I am trying to retrieve the first 3 characters of a field to either store or just query on direct.
I have the following two fields defined:
public String orderHeaderReason {get; set;}
public String orderHeaderReasonFirst3 {get; set;}
and then want to use them as follows:
// Save the reason specified on the OrderHeader - this works OK
orderHeaderReason = orderHeader.Reason_for_Order_FOC__c;
// Save the first 3 characters of the same field - this fails
orderHeaderReasonFirst3 = orderHeader.Reason_for_Order_FOC__c.substring(0,2);
//This also fails
orderHeaderReasonFirst3= orderHeaderReason.substring(0,2);
I am assuming that I have not correctly defined the fields orderHeaderReason and orderHeaderReasonFirst3 but cannot see what I should be doing - I need to instantiate them in some way?
Thanks
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
You might also notice that you could use the left(3) method in that documentation instead of substring(0,2);
All Answers
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
You might also notice that you could use the left(3) method in that documentation instead of substring(0,2);