title: Getting NullPointerException on HttpRequest execute() for sending POST request to Google Places API
body: <p>I am using Google Places API and trying to send a POST request to add a new place. But I keep getting a <code>NullPointerException</code> on the <code>request.execute()</code> part.</p>
  
  <pre><code>// Create POST body.
  GenericData data = new GenericData();
  GenericData coordinates = new GenericData();
  coordinates.put("lat", latitude);
  coordinates.put("lng", longitude);
  data.put("location", coordinates);
  data.put("accuracy", 50);
  data.put("name", name);
  JsonHttpContent content = new JsonHttpContent();
  content.data = data;
  
  // Send POST request.
  HttpRequestFactory httpRequestFactory = LocationUtil.createRequestFactory(transport);
  HttpRequest request = httpRequestFactory.buildPostRequest(new GenericUrl(GooglePlacesConsts.PLACES_REPORT_ADD_URL), content);
  request.url.put("sensor", "false");
  request.url.put("key", GooglePlacesConsts.API_KEY);
  HttpResponse response = request.execute();
  </code></pre>
  
  <p>The code for <code>createRequestFactory()</code> is (this is from <a href="http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html" rel="nofollow noreferrer">Davy's Tech Blog</a>):</p>
  
  <pre><code>public static HttpRequestFactory createRequestFactory(final HttpTransport transport) {
      return transport.createRequestFactory(new HttpRequestInitializer() {
          public void initialize(HttpRequest request) {
              GoogleHeaders headers = new GoogleHeaders();
              headers.setApplicationName("App Name");
              request.headers = headers;
              JsonHttpParser parser = new JsonHttpParser();
              parser.jsonFactory = new JacksonFactory();
              request.addParser(parser);
          }
      });
  }
  </code></pre>
  
  <p>Is the code wrong? I kinda borrowed lines of codes from different sources, because I...don't know how to correctly send a POST request in Java. Mainly, I followed the tutorial from <a href="http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html" rel="nofollow noreferrer">Davy's Tech Blog</a>, but also tried to follow <a href="https://stackoverflow.com/questions/4724270/how-to-post-request-to-google-shortener-api-with-google-api-java-client-and-parse">this post</a>, because the previous link provided examples only for sending GET requests, and I need to send a POST request.</p>
  
  <p>By the way, this is the stack trace:</p>
  
  <pre><code>javax.faces.FacesException: #{locationBean.actionAdd}: java.lang.NullPointerException
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Unknown Source)
  Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      ... 22 more
  Caused by: java.lang.NullPointerException
      at com.google.api.client.http.json.JsonHttpContent.writeTo(JsonHttpContent.java:75)
      at com.google.api.client.http.apache.ContentEntity.writeTo(ContentEntity.java:56)
      at org.apache.http.entity.HttpEntityWrapper.writeTo(HttpEntityWrapper.java:101)
      at org.apache.http.impl.client.EntityEnclosingRequestWrapper$EntityWrapper.writeTo(EntityEnclosingRequestWrapper.java:107)
      at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:127)
      at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:253)
      at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:218)
      at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:249)
      at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:483)
      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
      at com.google.api.client.http.apache.ApacheHttpRequest.execute(ApacheHttpRequest.java:58)
      at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:361)
      at model.location.LocationBean.actionAdd(LocationBean.java:57)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
      at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      ... 23 more
  </code></pre>
  
  <p>Thank you!</p>