I have a custom Obj, and try to realize a POST method.

But I get an Error: System.NullPointerException: null argument for JSONGenerator.writeStringField()

What is wrong here?

public static void postCalloutResponseContents() {   
    Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
                            FROM Settings__c
                            WHERE Name = 'OurSettings'];

    String consumerKey = settings.ConsumerKey__c;
    String consumerSecret = settings.ClientSecret__c;
    String username = settings.Username__c;
    String password = settings.Password__c + settings.SecurityToken__c;
    String requestBody = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
                         '&username=' + username + '&password='+password;

    Http ourHttp = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(System.Label.Job_Advertisement_URL + '/services/oauth2/token');

    Position__c pos = [SELECT Skills_Required__c FROM Position__c LIMIT 1];

    HttpResponse response = ourHttp.send(request);        
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(response.getbody(), OAuth2.class);
    System.debug('BODY: ' + response.getBody());

    if(objAuthenticationInfo.access_token != null){

        JSONGenerator gen = JSON.createGenerator(true);    
        gen.writeStringField('Position', pos.Skills_Required__c);
        String jsonS = gen.getAsString();
        System.debug('jsonMaterials: ' + jsonS);

        Http finalHttp = new Http();
        HttpRequest finalRequest = new HttpRequest();

        finalRequest.setHeader('Authorization','Bearer '+ objAuthenticationInfo.access_token);
        finalRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/apexrest/jobAdvertisementShow');
        HttpResponse finalResponse = finalHttp.send(finalRequest);
        System.debug('RESPONSE BODY: '+ finalResponse.getBody());

public class OAuth2{
    public String id{get;set;}
    public String issued_at{get;set;}
    public String instance_url{get;set;}
    public String signature{get;set;}
    public String access_token{get;set;}    
  • 4
    The error message seems pretty straightforward, no? Have you verified that the Skills_Required__c value that you're pulling is, in fact, not null?
    – Derek F
    Oct 2, 2018 at 16:33
  • I dont know how many Position records are in your Position object but since you're not running a very specific query, it is pulling a record with no Skills_Required__c. I would either alter the query to add a WHERE clause that says WHERE Skills_Required__c != null or add a null check to your gen.writeStringField line. Oct 2, 2018 at 16:43

1 Answer 1


All things being equal, the likely explanation is probably exactly what the error message is telling you (as Derek commented).

System.NullPointerException: null argument for JSONGenerator.writeStringField()

Position__c pos = [SELECT Skills_Required__c FROM Position__c LIMIT 1];
// ...
gen.writeStringField('Position', pos.Skills_Required__c);

We know pos isn't null itself, as that would have caused an error earlier when the SOQL query was executed (System.QueryException: List has no rows for assignment to SObject).

So that really only leaves the pos.Skills_Required__c argument as being the null argument.

There are two ways you could guard against this:

  1. Add an System.assertNotEquals(null, pos.Skills_Required__c, 'Skills_Required__c is null'); before you try and use the value. That will at least give you a meaningful error message if the value isn't there. If you expect this could actually occur with production data then throwing an exception would be better than the assertion.
  2. Guard against the problem data right at the SOQL query. There is a very real possibility that no rows will match this refined query, so you will either need to assign the results to a list or handle the QueryException.


Position__c pos = [SELECT Skills_Required__c 
                   FROM Position__c 
                   WHERE Skills_Required__c != null 
                   LIMIT 1];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .