-1

I am trying to add some specific ParseObject in my List based on the username of the current user. But this code only ended up in NullPointerException on lines:

query.findInBackground(new FindCallback()

mStatus.add(status.get(i));

Somebody please help me solve this. Thank you.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = null;
    view = inflater.inflate(R.layout.update_status, container, false);

    ParseUser currentUser = ParseUser.getCurrentUser();
    final String strCurrentUsername = currentUser.getUsername();
    if (currentUser != null) {

        //try
        final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status");
        query.orderByDescending("createdAt");

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(final List<ParseObject> status, ParseException e) {
                if(e == null){
                    //success
                    for(int i = 0; i <status.size();i++){
                        String usernow = status.get(i).getString("user");
                        if(usernow.equals(strCurrentUsername)){
                            Toast.makeText(getActivity(), "num"+i, Toast.LENGTH_SHORT).show();
                            mStatus.add(status.get(i));
                        }
                        else i++;
                    }
                    //mStatus = status;
                    StatusAdapter statusAdapter = new StatusAdapter( getListView().getContext(), mStatus);
                    setListAdapter(statusAdapter);

                }else{
                    //there was a problem

                }
            }
        });


    } else {
        Intent intent = new Intent(getActivity(), FirstActivity.class);
        startActivity(intent);
    }

    return view;
}
rici
  • 234,347
  • 28
  • 237
  • 341

1 Answers1

0

Is mStatus initalized? is mStatus a class field? I am not sure where you are getting the null exception but I tested your code and this worked for me where user is a column in my parse database and its value is a string:

 ParseUser currentUser = ParseUser.getCurrentUser();
    final String strCurrentUsername = currentUser.getUsername();
    if (currentUser != null) {

        //try
        final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status");
        query.orderByDescending("createdAt");

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(final List<ParseObject> status, ParseException e) {
                if (e == null) {
                    ArrayList<ParseObject> mStatus = new ArrayList<ParseObject>();
                    //success
                    for (int i = 0; i < status.size(); i++) {
                        String usernow = status.get(i).getString("user");
                        if (usernow.equals(strCurrentUsername)) {

                            mStatus.add(status.get(i));
                        } else i++;
                    }


                } else {
                    //there was a problem

                }
            }
        });


    } else {

    }
  • yes.. mStatus was initialized as a protected List .. that's odd. I still can't fix the error. – kellyan yvett Jan 25 '16 at 18:59
  • Have you tried making it public ? hmm, where is the null point coming from exactly? mStatus or usernow? what is the ACL of your parse class? –  Jan 25 '16 at 19:09
  • anyway, I solved it. thanks to you and to this line ArrayList mStatus = new ArrayList(); maybe .add is not applicable for List. thanks! – kellyan yvett Jan 25 '16 at 19:10
  • Great! I am glad you got it working. –  Jan 25 '16 at 19:13