function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeremiah SherrillJeremiah Sherrill 

String replacement regex System.NullPointerException: Attempt to de-reference a null object

Trying to return only the numbers from a phone number and remove extraneous data, works just fine, I removes brackets, lines, all clean code but when I run tests on my code I get back System.NullPointerException: Attempt to de-reference a null object.

 

 




     

String udp_billing_contact_number = serviceOrder[0].Billing_Contact_Phone_Number__c;
String udp_contact_number = udp_billing_contact_number.replaceAll('[^0-9]', '');

 
Best Answer chosen by Jeremiah Sherrill
Bryan Leaman 6Bryan Leaman 6
So you have a serviceOrder where the Billing_Contact_Phone_Number__c is missing?
Try replacing line 2 with:
String udp_contact_number = udp_billing_contact_number==null ? null : udp_billing_contact_number.replaceAll('[^0-9]', '');
 

All Answers

Bryan Leaman 6Bryan Leaman 6
So you have a serviceOrder where the Billing_Contact_Phone_Number__c is missing?
Try replacing line 2 with:
String udp_contact_number = udp_billing_contact_number==null ? null : udp_billing_contact_number.replaceAll('[^0-9]', '');
 
This was selected as the best answer
Jeremiah SherrillJeremiah Sherrill
I can't believe I didn't think of that.   Thank you so much for answering my very dumb question.  I greatly appreciate you taking the time.