0

I'm fetching contacts from database using AsyncTask. And want to show it in listview. But I'm facing a problem. Here is a code.

public class BackgroundWorker extends AsyncTask<String,Void,String> {
        private Context context;
        private ListView listView;
   BackgroundWorker(Context ctx){
    context = ctx;
   }
   BackgroundWorker(Context ctx, ListView lv){
     context = ctx;
     listView = lv;
   }
  // Other code
  @Override
  protected void onPostExecute(String result){
    // convert result string in name and phone array and pass to contentAdapet
    // Content adapter is extended from array adapter and used to display data in listview which is working fine.
               ContentAdapter contentAdapter = new ContentAdapter(context, names, phones);
            listView.setAdapter(contentAdapter);
  }
 // reset of code
}

MainActivity

        ListView listView = (ListView) findViewById(R.id.listView);
            BackgroundWorker backgroundWorker = new BackgroundWorker(this, listView);
            backgroundWorker.execute("argument");

Console Error

 E/AndroidRuntime: FATAL EXCEPTION: main
              Process: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

These two classes are different java file not within a same file. BackgroundWorker.java and MainActivity.java

  • set listView as this.listView = lv; in your constructor, and see if it works. – Chordin4tion Feb 21 '17 at 07:30
  • No this is not working, I tried it –  Feb 21 '17 at 07:35
  • Why are you passing listView in AsyncTask, just declare it in your main class and use it in onPostExecute(). – Chordin4tion Feb 21 '17 at 07:37
  • Sorry, what do you mean by use it in onPostExecute() if I try to access `findViewById` in onPostExecute() this is not working. I'm new in android can you please explain what exactly you are saying. –  Feb 21 '17 at 07:41
  • Declare your ListView outside onCreate of MainActivity and then initialize it in onCreate of MainActivity and use it in your AsyncTask...Currently you are using listView from your AsyncTask...Do not pass listView as an argument in your AsyncTask's Constructor... – Chordin4tion Feb 21 '17 at 07:46
  • It provide me `can not resolve symbol` if I try to access that variable which is in `MainActivity`. Sorry, these two classes are different java file not within a same file. `BackgroundWorker.java` and `MainActivity.java` –  Feb 21 '17 at 07:52
  • In case if you have some specific reason to do so, try using WeakReference. See usage here http://stackoverflow.com/a/29590774/2694480 so that you don't end up with memory leak. Also, try using callback model using interface to interact from AsyncTask to your MainActivity, where interface will be defined in MainActivity and AsyncTask can invoke it using reference to the callback. – Mithun Feb 21 '17 at 07:55

1 Answers1

0

I think you can't set the content view and it must be before backgroundWorker object. Or check there must be a listview in your main activity.

   setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.listView);
        BackgroundWorker backgroundWorker = new BackgroundWorker(this, listView);
        backgroundWorker.execute("argument");
Azeem Haider
  • 1,443
  • 4
  • 23
  • 41
  • Oh, sorry I set the content view after the BackgroundWorker object, that's why the are not able to get the listview. thanks –  Feb 21 '17 at 08:01