Facebook Graph java.lang.NullPointerException

时间:2017-12-30 11:47:51

标签: java android facebook facebook-graph-api

我制作了一个通过Facebook Graph获得一些Facebook帖子的应用程序。一切都在我的手机上很好用(Cubot Note S& UMI Diamond),但当我尝试在我的手机LG K10(2017)的其他手机上打开应用程序时。我收到错误:java.lang.NullPointerException:并且我不知道为什么,因为在第一部手机上这个应用程序运行良好。

java.lang.NullPointerException: Attempt to invoke virtual method 
'java.util.List com.gireada.***.Posts2.getData()' on a null object reference
at com.gireada.***.MainActivity$3$1.onCompleted(MainActivity.java:153)

这是

for (int i = 0; i < (variabila2.getPosts().getData().size()) - 1; i++)

每当我尝试访问variabila2.getPosts()....时,都会出现此错误:

前两款手机有6.0安卓和LG有7.0,这可能是个问题吗?

我使用此方法制作了GraphCall

GraphRequest request = GraphRequest.newMeRequest(
                                accessToken,
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                    System.out.println(object);
                                    Log.d("Profil ", object.toString());


globalObject.setCurentToken(AccessToken.getCurrentAccessToken());
                                    String nome;
                                        /*if (jObjResponse.has("name")) {
                                            nome = 

jObjResponse.getString("name");
                                                globalObject.setNume(nome);
                                            }
                                            if (jObjResponse.has("id")) {
                                                nome = jObjResponse.getString("id");
                                                globalObject.setID_User(nome);
                                            }*/
                                            variabila2 = new Gson().fromJson(object.toString(), Variable2.class);
    //                                            System.out.println(variabila2.getPosts().toString());
    //                                            Log.d("Link ", variabila2.getPosts().getData().get(0).getLink());
                                            for (int i = 0; i < (variabila2.getPosts().getData().size()) - 1; i++)
                                            {
                                                Log.d("Link ", variabila2.getPosts().getData().get(i).getLink());
                                                if (variabila2.getPosts().getData().get(i).getCreated_time() != null)
                                                    globalObject.setCreated_global(variabila2.getPosts().getData().get(i).getCreated_time(), i);
                                                else
                                                    globalObject.setCreated_global(null, i);

                                                globalObject.setLink_global(variabila2.getPosts().getData().get(i).getLink(), i);
                                                Log.d("Link Personal", variabila2.getPosts().getData().get(i).getLink());
                                                if (variabila2.getPosts().getData().get(i).getFull_picture() != null)
                                                    globalObject.setLink_pictures(variabila2.getPosts().getData().get(i).getFull_picture(), i);
                                                else
                                                    globalObject.setLink_pictures(null, i);
                                            }
                                        }
                                    });
                            Bundle parameters = new Bundle();
                            parameters.putString("fields", "id,name,posts.limit(100){link,created_time,description,full_picture}");
                            request.setParameters(parameters);
                            request.executeAsync();

**Variable2 class**

    public class Variable2
    {
        public String status;
        public Feed feed;
        Posts2 posts;
        Picture picture;
        public Variable2(Posts2 posts2) {
            this.posts = posts2;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public Feed getFeed() {
            return feed;
        }
        public void setFeed(Feed feed) {
            this.feed = feed;
        }
        public Posts2 getPosts() {
            return posts;
        }
        public void setPosts(Posts2 posts) {
            this.posts = posts;
        }
        public Picture getPicture() {
            return picture;
        }
        public void setPicture(Picture picture) {
            this.picture = picture;
        }
    }

**Post2 class**


    public class Posts2 {
        public List<Item2> data;

        public Posts2(List<Item2> data) {
            this.data = data;
        }

        public List<Item2> getData() {
            return data;
        }

        public void setData(List<Item2> data) {
            this.data = data;
        }
    }

**Item2 class**

    public class Item2 {
        public String link;
        public String created_time;
        public String full_picture;
        public String description;
        public String id;
        public Item2(String link, String created_time, String description, String full_picture, String id) {
            this.link = link;
            this.created_time = created_time;
            this.description = description;
            this.full_picture = full_picture;
            this.id = id;
        }

        public String getLink() {
            return link;
        }

        public void setLink(String link) {
            this.link = link;
        }

        public String getCreated_time() {
            return created_time;
        }

        public void setCreated_time(String created_time) {
            this.created_time = created_time;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getFull_picture() {
            return full_picture;
        }

        public void setFull_picture(String full_picture) {
            this.full_picture = full_picture;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

    }

这适用于我的第一部手机,但在这款手机上(可能在其他手机上),它无效。

如果你对这个问题有所了解,请帮助我。

1 个答案:

答案 0 :(得分:0)

我找到了答案。我没有把所有的权限都放在登录按钮上。

    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_posts", "public_profile", "email"));

感谢大家的帮助。这就是我只获得少量数据的原因。

  {"id":"2016399981939546","name":"Grigore Saveluc Rodica"}