杰克逊映射错误

时间:2015-11-11 16:07:44

标签: java json jackson

我无法将我的JSON文件读回相应的课程。我正在使用Jackson 2.6.3 API

foo = 1,2,3
foo
=> [1,2,3]

foo = 1,
2,
3
foo
=> [1,2,3]

我的ObjectMapper objectMapper = new ObjectMapper(); list<Movie>myMovies = objectMapper.readValue(JSONFile,objectMapper.getTypeFactory().constructCollectionType(List.class, Movie.class)); 班级

Movie

错误日志

public class Movie {
    private List<String> reviews;
    /*Some more variables here */

    public Movie() { // default constructor 
    }

    public List<String> getReviews(){
        return this.reviews;
    }
    public void setReviews(String review){
        this.reviews.add(review);       
    }
}

我的JSON文件示例

com.fasterxml.jackson.databind.JsonMappingException: N/A (through reference chain: java.util.ArrayList[0]->entity.Movie["reviews"])
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:564)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:548)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2658)
    at entity.Movie.loadMovies(Movie.java:427)
    at myPackage.Application.main(Application.java:31)
Caused by: java.lang.NullPointerException
    at entity.Movie.setReviews(Movie.java:45)
    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 com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:97)
    ... 9 more

0 个答案:

没有答案