in Async Task: Attempting to invoke the virtual method 'void android.widget.ListView.setAdapter (android.widget.ListAdapter)' on a null object reference

advertisements

What i want

When click on Marker in Map, Show alert dialog with list view (containing text)

LogCat Error

05-01 23:20:21.098 11392-11654/com.example.talha.atmlocaterproject W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at com.example.talha.atmlocaterproject.MainActivity$override.retreive_message(MainActivity.java:372)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at com.example.talha.atmlocaterproject.MainActivity$override.access$dispatch(MainActivity.java)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at com.example.talha.atmlocaterproject.MainActivity.retreive_message(MainActivity.java:0)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at com.example.talha.atmlocaterproject.MainActivity$GetAtms.doInBackground(MainActivity.java:300)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at com.example.talha.atmlocaterproject.MainActivity$GetAtms.doInBackground(MainActivity.java:257)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-01 23:20:21.099 11392-11654/com.example.talha.atmlocaterproject W/System.err:     at java.lang.Thread.run(Thread.java:818)

What i am doing is in OnMarkerClick() is calling AsyncTask to get message from cloud.

@Override
public boolean onMarkerClick(Marker marker) {
    String atm_title = marker.getTitle();
    option = "retreive_message";
    new GetAtms(atm_title).execute();      //here is the call to asyncTask
    return false;
}

Now in doInBackground() method , i retreive the message from cloud and set adapter

DBCollection c5  = db.getCollection("ATM_message");
    String[] atm_message = new String[50];
    try{
        Log.d("retreive message","before lopping");
        DBCursor cursor = c5.find();
        String atm_title,message,username;
        int message_adding_counter = 0;
        while (cursor.hasNext()) {
            DBObject doc = cursor.next();
            atm_title = "" + doc.get("atm_name");
            message = "" + doc.get("message");
            username ="" + doc.get("username");
            Log.d("retreive message","between lopping");

            if(atm_title.equalsIgnoreCase(title)){
                atm_message[message_adding_counter]=message;
                Log.d("retreive message"," adding message");

            }
        }
        Log.d("retreive message","" + atm_message[0]);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.textView,atm_message);
        listview.setAdapter(adapter);
        showDailogListView(listview);

showDailogListView()

public void showDailogListView(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setCancelable(true);
    builder.setPositiveButton("OK",null);
    builder.setView(view);
    AlertDialog dailog = builder.create();
    dailog.show();
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:padding="10dp"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

OnCreate()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mProgressDialog= new ProgressDialog(this);

    try {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    } catch (Exception e) {
        e.printStackTrace();
    }

}


Try to use onPostExecute to deal with your UI:

Change your signature of AsyncTask:

public class GetAtms extends AsyncTask<Void, Void, String[]> {
    private ArrayAdapter<String> mAdapter;

    public GetAtms(String title, ArrayAdapter<String> adapter)
    {
        mAdapter = adapter;
        //the rest of the constructor codes
    }

    //rest of your code

let your doInBackground return the result String array to onPostExecute

DBCollection c5  = db.getCollection("ATM_message");
String[] atm_message = new String[50];
try{
    Log.d("retreive message","before lopping");
    DBCursor cursor = c5.find();
    String atm_title,message,username;
    int message_adding_counter = 0;
    while (cursor.hasNext()) {
        DBObject doc = cursor.next();
        atm_title = "" + doc.get("atm_name");
        message = "" + doc.get("message");
        username ="" + doc.get("username");
        Log.d("retreive message","between lopping");

        if(atm_title.equalsIgnoreCase(title)){
            atm_message[message_adding_counter]=message;
            Log.d("retreive message"," adding message");

        }
    }
    Log.d("retreive message","" + atm_message[0]);

    return atm_message;

in onPostExecute do:

public void onPostExecute(String[] data)
{
    mAdapter.clear();
    mAdapter.addAll(data);
}

and finally change your onMarkerClick:

public boolean onMarkerClick(Marker marker) {
    String atm_title = marker.getTitle();
    option = "retreive_message";
    ListView listview = (ListView) findViewById(...) //your listview resource
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.textView,null);
    listView.setAdapter(adapter);
    new GetAtms(atm_title,adapter).execute();      //here is the call to asyncTask
    return false;

}