开发者

Getting NullPointerException on HttpRequest execute() for sending POST request to Google Places API

I am using Google Places API and trying to send a POST request to add a new place. But I keep getting a NullPointerException on the request.execute() part.

// 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();

The code for createRequestFactory() is (this is from Davy's Tech Blog):

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);
        }
    });
}

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 Davy's Tech Blog, but also tried to follow this post, because the previous link provided examples only for sending GET requests, and I need to send a POST request.

By the way, this is the stack trace:

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.i开发者_如何学Gonvoke(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

Thank you!


I struggled to find a version of that class that had a line number valid for your exception, but I found 1.4.1-beta.

public void writeTo(OutputStream out) throws IOException {
    JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
    generator.serialize(data);
    generator.flush();
}

Line 45 is the first line of the method. So check that jsonFactory is not null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜