Error in Android JsonDeserializer when returning response

对着背影说爱祢 提交于 2019-12-12 06:46:15

问题


im new here and i have a problem i can not solve.

im getting this error in the debug in execution time:

05-02 11:56:07.539  18645-24466/com.appss.appssapp D/Retrofit﹕ java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0, v2}, java.util.ArrayList com.appss.appssapp.io.model.PostsResponse$MainResponse.access$002(com.appss.appssapp.io.model.PostsResponse$MainResponse, java.util.ArrayList) // method@19256'
at com.appss.appssapp.io.model.PostsResponse.setPosts(PostsResponse.java:21)
            at com.appss.appssapp.io.deserializer.PostsResponseDeserializer.deserialize(PostsResponseDeserializer.java:31)
            at com.appss.appssapp.io.deserializer.PostsResponseDeserializer.deserialize(PostsResponseDeserializer.java:21)
            at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
            at com.google.gson.Gson.fromJson(Gson.java:810)
            at com.google.gson.Gson.fromJson(Gson.java:775)
            at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
            at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
            at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
            at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at retrofit.Platform$Android$2$1.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:818)

the app is compiled correctly, run without a problem, but when i make the calls to the api doesn't show the data, i mean, the api send the data, and i do what i have to do with it, but when i try to send de data to the response in the debug shows me the problem i wrote up above.

this is my code.

Posts.JAVA

public class Posts {
    String id; //Id de la publicacion
    String titulo; // Titulo de la publicacion
    String url; // Url de la publicacion
    String imagen; // Imagen cover la publicacion
    String autor_nick; // Nick del autor de la publicacion
    String visitas; // Lecturas de la publicacion
    String comentarios; // Cantidad de comentarios de la publicacion
    public Posts () {}
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTitulo() {
        return titulo;
    }
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getImagen() {
        return imagen;
    }
    public void setImagen(String imagen) {
        this.imagen = imagen;
    }
    public String getAutor_nick() {
        return autor_nick;
    }
    public void setAutor_nick(String autor_nick) {
        this.autor_nick = autor_nick;
    }
    public String getVisitas() {
        return visitas;
    }
    public void setVisitas(String visitas) {
        this.visitas = visitas;
    }
    public String getComentarios() {
        return comentarios;
    }
    public void setComentarios(String comentarios) {
        this.comentarios = comentarios;
    }
}

PostsResponse.JAVA

public class PostsResponse {

    private MainResponse mainResponse;

    public ArrayList<Posts> getPosts(){
        return  mainResponse.posts;
    }

    public void setPosts(ArrayList<Posts> posts) {
        mainResponse.posts = posts;
    }

    private class MainResponse {

        private ArrayList<Posts> posts;

    }
}

PostResponseDeserializer.JAVA

public class PostsResponseDeserializer implements JsonDeserializer<PostsResponse> {
    @Override
    public PostsResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Gson gson = new Gson();
        PostsResponse response = gson.fromJson(json, PostsResponse.class);

        JsonObject postsResponseData = json.getAsJsonObject();

        JsonArray postsArray = postsResponseData.getAsJsonArray("posts");

        response.setPosts(extractPostsFromJsonArray(postsArray));

        return response;
    }

    private ArrayList<Posts> extractPostsFromJsonArray(JsonArray postsArray) {
        ArrayList<Posts> posts = new ArrayList<>();
        for (int i = 0; i < postsArray.size(); i++) {

            JsonObject postsDatos = postsArray.get(i).getAsJsonObject();

            Posts actualPost = new Posts();
            String titulo = postsDatos.get("titulo").getAsString();
            String imagen = postsDatos.get("imagen_370x208").getAsString();
            String visitas = postsDatos.get("visitas").getAsString();

            actualPost.setTitulo(titulo);
            actualPost.setImagen(imagen);
            actualPost.setVisitas(visitas);

            posts.add(actualPost);
        }
        return posts;
    }
}

I think the problem might be here response.setPosts(extractPostsFromJsonArray(postsArray));

but i can't figure out why is this happening.

Any help? .. And sorry for my bad english.


回答1:


I am not sure but check this.

When you call setposts method it will try to initialize an arraylist while the object that contains the list is not initialized. So you should new an instance of MainResponse first.



来源:https://stackoverflow.com/questions/36989026/error-in-android-jsondeserializer-when-returning-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!