title: NullPointerException only after I have setServiceAccountUser() for GoogleCredential object (Grails/Java)
body: <p>I receive a <code>NullPointerException</code> with very little detail when I attempt to <code>setServiceAccountUser(ACCOUNT_TO_IMPERSONATE)</code> on my <code>GoogleCredential</code>, build a Google Tasks Service Object and subsequently try to send a request for a list of Tasks from <code>ACCOUNT_TO_IMPERSONATE</code>.</p>
  
  <pre><code>def credential = new GoogleCredential.Builder()
      .setTransport(HTTP_TRANSPORT)
      .setJsonFactory(JSON_FACTORY)
      .setServiceAccountId(SERVICE_ACCOUNT_ID)
      .setServiceAccountPrivateKeyFromP12File(P12_FILE)
      .setServiceAccountScopes(GOOGLE_TASK_SCOPES)
      .setServiceAccountUser(ACCOUNT_TO_IMPERSONATE)
      .build()
  
      credential.refreshToken()
      log.debug("Google Credential Impersonating: ${credential.getServiceAccountUser()}")
  
      def service = new com.google.api.services.tasks.Tasks.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build()
  
      return service
  </code></pre>
  
  <p>Please note that the <code>credential.getServiceAccountUser()</code> logs out the expected <code>ACCOUNT_TO_IMPERSONATE</code>.</p>
  
  <p>There was a similar issue reported in <a href="https://github.com/googleads/googleads-java-lib/issues/19" rel="nofollow">https://github.com/googleads/googleads-java-lib/issues/19</a></p>
  
  <pre><code>StackTrace :
  Caused by: java.lang.NullPointerException at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
  at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
  at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
  at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
  at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
  at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
  at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
  at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
  at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
  at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:247)
  at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
  at com.google.api.ads.common.lib.auth.OAuth2Helper.callRefreshToken(OAuth2Helper.java:70)
  at com.google.api.ads.common.lib.auth.OfflineCredentials.generateCredential(OfflineCredentials.java:144)
  ...
  </code></pre>
  
  <p>The lack of detail in the error was said to be an issue in older versions of Google Tasks but I have upgraded to the latest version and see no difference.</p>
  
  <pre><code>compile "com.google.api-client:google-api-client:1.19.0"
  compile 'com.google.apis:google-api-services-tasks:v1-rev41-1.19.0'
  </code></pre>
  
  <p>EDIT: After tinkering with the code in the grails console in our web application I can see that executing <code>credential.refreshToken()</code> causes the NPE when <code>ServiceAccountUser</code> is set. </p>
  
  <p>When I unset the <code>ServiceAccountUser</code>, <code>refreshToken()</code> is successful. Additionally, trying to retrieve a list of Google Tasks is succcessful for the Service account when the <code>ServiceAccountUser</code> is not set.</p>
  
  <p>Any help would be greatly appreciated.</p>