Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Why is the LogCat showing NullPointerException when I use ArrayAdapter?

Ribbit App is showing NullPointer Exception when I use the array adapter. If i comment the statement setListAdapter(), the app is not crashing but this does not display the contents which I intent to display. Also, I created a similar fragment in another app, but there it does not crash. Please help me fix this out. The fragment code is

public class InboxFragment extends ListFragment {
protected ArrayList<ParseObject> myMessages = new ArrayList<ParseObject>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_inbox, container,
            false);
    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
            ParseConstants.KEY_MESSAGES);
    query.whereEqualTo(ParseConstants.KEY_RECEPIENTSIDS, ParseUser
            .getCurrentUser().getObjectId());
    query.addDescendingOrder("createdAt");

    query.findInBackground(new FindCallback<ParseObject>() {


        @Override
        public void done(List<ParseObject> arg0, ParseException e) {
            if (e == null) {
                // success
                Toast.makeText(getListView().getContext(), "Loaded messages!",
                        Toast.LENGTH_LONG).show();

                String usernames[] = new String[arg0.size()];
                for( int i=0; i<usernames.length; i++)
                {
                    usernames[i] = arg0.get(i).getString(ParseConstants.KEY_SENDERNAME);
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView()
                        .getContext(), android.R.layout.simple_list_item_1, usernames
                        );
                setListAdapter(adapter);

            } else {
                // failure
                Toast.makeText(getListView().getContext(), "Failed to load messages!",
                        Toast.LENGTH_LONG).show();


            }

        }
    });

}

Thanks in advance