Nejma Boubakri
Nejma Boubakri

Reputation: 1

cannnot save the result of parse query in an android variabe

I am currently using Parse.com's API for an android application. I want to retreive an array from parse and save it as a list to work with later. but tha problem is when i'm out of the query my list turnes empty. can u help me?

ParseUser currentUser = ParseUser.getCurrentUser();
        String userId= currentUser.getObjectId();
        final ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("Friendship");
        pQuery.whereEqualTo("user", userId);
        pQuery.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, ParseException e) {
                if (e==null) {
                    if (list.size()>0) {
                        ParseObject p = list.get(0);
                        if (p.getList("friendsList")!=null) {
                            list11 =  p.getList("friendsList");
                            Toast.makeText(getActivity().getApplicationContext(),
                                    "Posted With Success  "+ list11.get(2).toString(),
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        });
        Toast.makeText(getActivity().getApplicationContext(),
                "Posted With Success  "+list11.get(1).toString(),
                Toast.LENGTH_LONG).show();

the first Toast is working but the second one gives me a NullPointerException.

Upvotes: 0

Views: 120

Answers (1)

Bhuvnesh
Bhuvnesh

Reputation: 1055

You can only put second Toast in done method like this:-

   @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e==null) {
                if (list.size()>0) {
                    ParseObject p = list.get(0);
                    if (p.getList("friendsList")!=null) {
                        list11 =  p.getList("friendsList");
                        Toast.makeText(getActivity().getApplicationContext(),
                                "Posted With Success  "+ list11.get(2).toString(),
                                Toast.LENGTH_LONG).show();
                        Toast.makeText(getActivity().getApplicationContext(),
                                "Posted With Success  "+list11.get(1).toString(),
                                Toast.LENGTH_LONG).show();

                    }
                }
            }
        }

Because findInBackground method start a new background thread.when you show second toast then list11 may be empty. Due to this it throws a NullPointerException

Upvotes: 1

Related Questions