Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JSON响应绑定到对象将返回NullPointerException_Java_Json_Nullpointerexception_Jackson - Fatal编程技术网

Java 将JSON响应绑定到对象将返回NullPointerException

Java 将JSON响应绑定到对象将返回NullPointerException,java,json,nullpointerexception,jackson,Java,Json,Nullpointerexception,Jackson,我试图使用Jakson库将以下JSON响应从绑定到对象,但它返回java.lang.NullPointerException 响应 {"region": {"span": {"latitude_delta": 0.10262262794520893, "longitude_delta": 0.22282942}, "center": {"latitude": 51.510372893357001, "lo

我试图使用Jakson库将以下JSON响应从绑定到对象,但它返回java.lang.NullPointerException

响应

 {"region": 
              {"span": 
                  {"latitude_delta": 0.10262262794520893, "longitude_delta": 0.22282942}, 
          "center": {"latitude": 51.510372893357001, "longitude": -0.1108336}}, 
      "total": 246, 
      "businesses": [
           {"is_claimed": false, 
               "rating": 4.5, 
               "mobile_url": "http://m.yelp.co.uk/biz/r-garcia-and-sons-foods-and-wines-of-spain-london", 
               "rating_img_url": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png", 
               "review_count": 11, 
               "name": "R Garcia \u0026 Sons - Foods and Wines of Spain", 
               "snippet_image_url": "http://s3-media2.ak.yelpcdn.com/photo/8xquccU5FKaFg9ZlANdOJA/ms.jpg", 
               "rating_img_url_small": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png", 
               "url": "http://www.yelp.co.uk/biz/r-garcia-and-sons-foods-and-wines-of-spain-london", 
               "phone": "+442072216119", 
               "snippet_text": "aka R Garcia \u0026 Son, and Cafe Garcia. Although, technically, Cafe Garcia is next door attached to their la carniceria. \n\nIt is a great Spanish grocery store...", 
               "image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/t14haXQrAW8HMwc0RTL1jQ/ms.jpg", 
               "categories": [["Delis", "delis"], ["Spanish", "spanish"], ["Beer, Wine \u0026 Spirits", "beer_and_wine"]], 
               "display_phone": "+44 20 7221 6119", 
               "rating_img_url_large": "http://s3-media4.ak.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png", 
               "id": "r-garcia-and-sons-foods-and-wines-of-spain-london", 
               "is_closed": false, 
               "location": 
                       {"city": "London", 
                   "display_address": ["248-250 Portobello Road", "Notting Hill", "London W11 1LL", "UK"], 
                   "neighborhoods": ["Notting Hill"], 
                   "postal_code": "W11 1LL", 
                   "country_code": "GB", 
                   "address": ["248-250 Portobello Road"], 
                   "state_code": "XGL"}
                        }, 
          {"is_claimed": false, "rating": 3.0, ......
        InputStream response = yelp.search("spanish+food", "London");

        ObjectMapper mapper = new ObjectMapper();
        Response responseObj= mapper.readValue(response, Response.class);
        System.err.println(">" + responseObj.getRegion().getSpan().getLatitude_delta()); 
我的对象

public class Response {
    private Region region;
    private int total;
    private List<Businesses> businesses = null;


    public Response() {
        this.businesses = new ArrayList();
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<Businesses> getBusinesses() {
        return businesses;
    }

    public void setBusinesses(List<Businesses> businesses) {
        this.businesses = businesses;
    }


}
public class Location {

    private String city;
    private String[] display_address;
    private String[] neighborhoods;
    private String postal_code;
    private String country_code;
    private String[] address;
    private String state_code;

    public Location() {
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String[] getDisplay_address() {
        return display_address;
    }

    public void setDisplay_address(String[] display_address) {
        this.display_address = display_address;
    }

    public String[] getNeighborhoods() {
        return neighborhoods;
    }

    public void setNeighborhoods(String[] neighborhoods) {
        this.neighborhoods = neighborhoods;
    }

    public String getPostal_code() {
        return postal_code;
    }

    public void setPostal_code(String postal_code) {
        this.postal_code = postal_code;
    }

    public String getCountry_code() {
        return country_code;
    }

    public void setCountry_code(String country_code) {
        this.country_code = country_code;
    }

    public String[] getAddress() {
        return address;
    }

    public void setAddress(String[] address) {
        this.address = address;
    }

    public String getState_code() {
        return state_code;
    }

    public void setState_code(String state_code) {
        this.state_code = state_code;
    }


}
位置类别

public class Response {
    private Region region;
    private int total;
    private List<Businesses> businesses = null;


    public Response() {
        this.businesses = new ArrayList();
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<Businesses> getBusinesses() {
        return businesses;
    }

    public void setBusinesses(List<Businesses> businesses) {
        this.businesses = businesses;
    }


}
public class Location {

    private String city;
    private String[] display_address;
    private String[] neighborhoods;
    private String postal_code;
    private String country_code;
    private String[] address;
    private String state_code;

    public Location() {
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String[] getDisplay_address() {
        return display_address;
    }

    public void setDisplay_address(String[] display_address) {
        this.display_address = display_address;
    }

    public String[] getNeighborhoods() {
        return neighborhoods;
    }

    public void setNeighborhoods(String[] neighborhoods) {
        this.neighborhoods = neighborhoods;
    }

    public String getPostal_code() {
        return postal_code;
    }

    public void setPostal_code(String postal_code) {
        this.postal_code = postal_code;
    }

    public String getCountry_code() {
        return country_code;
    }

    public void setCountry_code(String country_code) {
        this.country_code = country_code;
    }

    public String[] getAddress() {
        return address;
    }

    public void setAddress(String[] address) {
        this.address = address;
    }

    public String getState_code() {
        return state_code;
    }

    public void setState_code(String state_code) {
        this.state_code = state_code;
    }


}
例外情况

SEVERE:   org.codehaus.jackson.map.JsonMappingException: 
         (was java.lang.NullPointerException) (through reference chain:  
              com.myproject.restaurant.yelp.Response["businesses"])
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
    at org.codehaus.jackson.map.deser.BeanDeserializer.wrapAndThrow(BeanDeserializer.java:1472)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:699)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1909)
    at com.myproject.controller.Restaurants.Yelp(Restaurants.java:93)
    at com.myproject.controller.Restaurants.find(Restaurants.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzl
SEVERE:   y.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at org.codehaus.jackson.map.deser.impl.BeanPropertyMap.find(BeanPropertyMap.java:80)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:694)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
    at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
    ... 86 more
我的绑定代码

 {"region": 
              {"span": 
                  {"latitude_delta": 0.10262262794520893, "longitude_delta": 0.22282942}, 
          "center": {"latitude": 51.510372893357001, "longitude": -0.1108336}}, 
      "total": 246, 
      "businesses": [
           {"is_claimed": false, 
               "rating": 4.5, 
               "mobile_url": "http://m.yelp.co.uk/biz/r-garcia-and-sons-foods-and-wines-of-spain-london", 
               "rating_img_url": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png", 
               "review_count": 11, 
               "name": "R Garcia \u0026 Sons - Foods and Wines of Spain", 
               "snippet_image_url": "http://s3-media2.ak.yelpcdn.com/photo/8xquccU5FKaFg9ZlANdOJA/ms.jpg", 
               "rating_img_url_small": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png", 
               "url": "http://www.yelp.co.uk/biz/r-garcia-and-sons-foods-and-wines-of-spain-london", 
               "phone": "+442072216119", 
               "snippet_text": "aka R Garcia \u0026 Son, and Cafe Garcia. Although, technically, Cafe Garcia is next door attached to their la carniceria. \n\nIt is a great Spanish grocery store...", 
               "image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/t14haXQrAW8HMwc0RTL1jQ/ms.jpg", 
               "categories": [["Delis", "delis"], ["Spanish", "spanish"], ["Beer, Wine \u0026 Spirits", "beer_and_wine"]], 
               "display_phone": "+44 20 7221 6119", 
               "rating_img_url_large": "http://s3-media4.ak.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png", 
               "id": "r-garcia-and-sons-foods-and-wines-of-spain-london", 
               "is_closed": false, 
               "location": 
                       {"city": "London", 
                   "display_address": ["248-250 Portobello Road", "Notting Hill", "London W11 1LL", "UK"], 
                   "neighborhoods": ["Notting Hill"], 
                   "postal_code": "W11 1LL", 
                   "country_code": "GB", 
                   "address": ["248-250 Portobello Road"], 
                   "state_code": "XGL"}
                        }, 
          {"is_claimed": false, "rating": 3.0, ......
        InputStream response = yelp.search("spanish+food", "London");

        ObjectMapper mapper = new ObjectMapper();
        Response responseObj= mapper.readValue(response, Response.class);
        System.err.println(">" + responseObj.getRegion().getSpan().getLatitude_delta()); 

以下JSON字符串:

"categories": [["Delis", "delis"], ["Spanish", "spanish"], ["Beer, Wine \u0026 Spirits", "beer_and_wine"]],
不映射到

private String[] categories;
JSON
categories
是字符串列表,而您只是将其映射到一维字符串数组


尝试二维数组或最好使用以下JSON字符串:

"categories": [["Delis", "delis"], ["Spanish", "spanish"], ["Beer, Wine \u0026 Spirits", "beer_and_wine"]],
不映射到

private String[] categories;
JSON
categories
是字符串列表,而您只是将其映射到一维字符串数组


尝试二维数组或最好使用以下JSON字符串:

"categories": [["Delis", "delis"], ["Spanish", "spanish"], ["Beer, Wine \u0026 Spirits", "beer_and_wine"]],
不映射到

private String[] categories;
JSON
categories
是字符串列表,而您只是将其映射到一维字符串数组


尝试二维数组或最好使用以下JSON字符串:

"categories": [["Delis", "delis"], ["Spanish", "spanish"], ["Beer, Wine \u0026 Spirits", "beer_and_wine"]],
不映射到

private String[] categories;
JSON
categories
是字符串列表,而您只是将其映射到一维字符串数组


要么尝试二维数组,要么最好是使用
List

检查JSON的结构,我不确定它是否真的表示响应类中的对象。似乎区域包含业务,在您的类中它们都是分离的字段。检查JSON的结构,我不确定它是否真的表示响应类中的对象。似乎区域包含业务,在您的类中它们都是分离的字段。检查JSON的结构,我不确定它是否真的表示响应类中的对象。似乎区域包含业务,在您的类中它们都是分离的字段。检查JSON的结构,我不确定它是否真的表示响应类中的对象。似乎区域包含业务,在您的类中,它们都是分离的字段。