0

with fragments i got confused on how to call the UI compared to activities as i have to do it differently. Now with my listview i am initialising it in the oncreateView as most of the tutorials state that i have to do. however on my PostExecute function it's seems to return an empty ui and therefore the results will be null which triggers a null exception error.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.listview_main, container, false);
    listview = (ListView) view.findViewById(R.id.listview);

    new RemoteDataTask().execute();
    return view;
}




private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(getActivity());
        // Set progressdialog title
        mProgressDialog.setTitle("Stored Files");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Locate the class table named "UploadedFiles" in Parse.com
        final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("NewFiles");
        query.orderByDescending("_created_at");
        query.findInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> objects, com.parse.ParseException e) {

                if (e == null) {
                    try {
                        aF = query.find();
                    } catch (com.parse.ParseException e1) {
                        e1.printStackTrace();
                    }
                    Toast.makeText(getActivity(), "Success",
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d("Error", e.toString());
                }
            }


        });

        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml

        // Pass the results into an ArrayAdapter
        adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_view_item);
        // Retrieve object "ImageName" from Parse.com database
        for (ParseObject NewFiles : aF) {
            adapter.add((String) NewFiles.get("ImageName"));
        }
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
        // Capture button clicks on ListView items
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // Send single item click data to SingleItemView Class
                Intent i = new Intent(getActivity(), SingleFileView.class);
                // Pass data "name" followed by the position
                i.putExtra("ImageName", aF.get(position).getString("ImageName")
                        .toString());
                // Open SingleItemView.java Activity
                startActivity(i);
            }
        });
    }
}

And this is the error that i am getting. And I have the Listview set correctly with no mistakes so i don't think the issue is from that.

Process: com.venomdev.safestorage, PID: 3027
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
at com.venomdev.safestorage.ListViewFiles$RemoteDataTask.onPostExecute(ListViewFiles.java:146)
at com.venomdev.safestorage.ListViewFiles$RemoteDataTask.onPostExecute(ListViewFiles.java:95)
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Anasbzr
  • 59
  • 2
  • 10

3 Answers3

1

You're not verifying that aF is not null before dereferencing it. Whether you should do a null check, or exit the function if it is null, is up to you.

You can try this

    if(aF != null)
    {
        for (ParseObject NewFiles : aF) 
        {
            adapter.add((String) NewFiles.get("ImageName"));
        }
    }

Also, in your doInBackground function, you are reporting "success" no matter what happens. You should do something like this:

               if (e == null) {
                    try {
                        aF = query.find();
                    } catch (com.parse.ParseException e1) {
                        e1.printStackTrace();
                        return; //<-- add this, and if you wish, an eror message
                    }
                    Toast.makeText(getActivity(), "Success",
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d("Error", e.toString());
                }
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
1

At this point your variable aF was not initialized

 for (ParseObject NewFiles : aF) {
            adapter.add((String) NewFiles.get("ImageName"));
        }

Probably e is null :

  if (e == null) {
                    try {
                        aF = query.find();
                    } catch (com.parse.ParseException e1) {
                        e1.printStackTrace();
                    }
                    Toast.makeText(getActivity(), "Success",
                            Toast.LENGTH_LONG).show();
                } else {
                    Log.d("Error", e.toString());
                }

and this line is never executed:

aF = query.find();
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    i initialised it in the beginning of the class as "private static List aF = null;". Thank you it seems to be working now – Anasbzr Dec 11 '15 at 17:52
1

A NullPointerException usually means you tried to call a method off of an object that may have been declared, but not yet set. Without seeing the code, I would guess that you tried to call a method off of an arrayList, (perhaps in for loop) but the arrayList was not yet instantiated.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26