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