There are group of fields I am querying and putting them in JSON. Some of these fields may be blank which is making previous test classes fail throwing a nul pointer exception.

**Present Code ** There are many other fields. I am just putting a sample

        gen.writeStringField('DayPhone', c.contact.OtherPhone);
        gen.writeStringField('EveningPhone', c.contact.HomePhone);
        gen.writeStringField('WorkPhone', c.contact.phone), c.account.Billing_Account_Number__c);

Currently, I have to write if condition like this for it to work:

    if(c.contact.HomePhone != null){gen.writeStringField('EveningPhone', c.contact.HomePhone);}
    if(c.contact.phone != null){gen.writeStringField('WorkPhone', c.contact.phone);}
    if(c.contact.MobilePhone != null){gen.writeStringField('Mobile', c.contact.MobilePhone);}

Since I am adding if on every line, I wanted to know if any check can be done all at once or any other option.

**Update ** I found that using JSON serialize can avoid this exception. So I am planning to change my code from JSON Generator to serialize instead of writing if condition on all fields. But is there a way to add if condition while serializing.

Ex: When I use JSONGenerator I can add if condition like this:

if(c.accountId != null){ gen.writestringfield(c.accountId); }

which adds this value to json only if accountid is not null. But in case of serialize, it is

public class caseVars {
           public string caseId;
        public string accountId;

public static void postCases (list<case> c1) {
   for(case c: c1){
            caseVars caseVar = new caseVars();
            caseVar.caseId = IdSplit(c.Id); 
     if(c.accountId != null){ caseVar.accountId = c.accountId; }

I believe in the second method, evenif accountId is null, there is a label named accountId with null value in the Jsonstring. Is there a way to control that ? ( Is there a way not to show accountId field at all in JSON string if there is no accountId on case)


1 Answer 1


One of the options is to have a map of the API fields on the contact record to their corresponding key values for your JSON string. Then you could put your Contact record into an sObject variable and use the .get method to dynamically retrieve a field value based on the API name:

sObject contactSObject = c.contact;

Map <String, String> fieldNames = new Map <String, String> ('HomePhone' => 'EveningPhone', 'Phone' => 'WorkPhone', 'MobilePhone' => 'Mobile');

for (String fieldName : fieldNames.keySet())
    if (contactSObject.get(fieldName) != null)
        gen.writeStringField(fieldNames.get(fieldName), (String)contactSObject.get(fieldName));

Be careful with the types as not all fields return String. I'll leave that to you to sort out.

  • Thansk Bachovski... Another option I found is when I use serialize instead of JSONGenerator, I am not getting the null exception. May be I have to change my entire code as there are fields other than contacts also...
    – sf_user
    Aug 10, 2015 at 2:16
  • 1
    Yeah that's another option, you could serialize the object and the run a replace method on the string Aug 10, 2015 at 2:28
  • Thanks Bachovski, I am trying to change to serialize. Is there a way to ontrol the fields in json string when using serialize ? (updated code in the question)
    – sf_user
    Aug 10, 2015 at 3:14
  • 1
    Unfortunately no - you can't control the fields when you use the serialize method. You need to run some regex on the output string in order to remove them. A good example can be found here - iwritecrappycode.wordpress.com/2014/07/16/… Aug 10, 2015 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.