0

I'm trying to use the cursor adapter but I have a null pointer exception when I call onPostExecute, I can't figure out what the problem is. Main idea of activity is display list of contacts. Code:

Main activity:

public class AddressBook extends ListActivity {    
    public static final String ROW_ID = "row_id"; // additional Intent key
    private ListView contactListView; // ListView component from ListActivity
    private CursorAdapter contactAdapter; //CursorAdapter for ListView    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        contactListView = getListView(); // access to ListView
        contactListView.setOnItemClickListener(viewContactListener);

        // display contact name on TextView in ListView
        String[] from = new String[]{"name"};
        int[] to = new int[]{R.id.contactTextView};
        CursorAdapter contactAdapter = new SimpleCursorAdapter(AddressBook.this, R.layout.contact_list_item, null, from, to);
        setListAdapter(contactAdapter); // setting adaptercontactView
    }

    // creating Activity menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.addressbook_menu, menu);
        return true;
    }

    // Selectng option
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {    
        //create new Intent object for AddEditContact method
        Intent addNewContact =
                new Intent(AddressBook.this, AddEditContact.class);
        startActivity(addNewContact); // start the AddEditContact Activity
        return super.onOptionsItemSelected(item); // call superclass method
    }

    @Override
    protected void onResume() {
        super.onResume(); 
        // Create new GetContactsTask object and call it
        new GetContactsTask().execute((Object[]) null);
    }

    @Override
    protected void onStop() {
        Cursor cursor = contactAdapter.getCursor(); // access to current Cursor    
        if (cursor != null)
            cursor.deactivate();      // deactivating    
        contactAdapter.changeCursor(null); // change Cursor to null
        super.onStop();
    }

    private class GetContactsTask extends AsyncTask<Object, Object, Cursor> {    
        DatabaseConnector databaseConnector =
                new DatabaseConnector(AddressBook.this); // performing access to database    
        @Override
        protected Cursor doInBackground(Object... params) {
            databaseConnector.open(); // access to Cursor
            return databaseConnector.getAllContacts();
        }  

        @Override
        protected void onPostExecute(Cursor result) {  // using Cursor, returned by doInBackground method
            contactAdapter.changeCursor(result); // change Cursor adapter //NULL POINTER EXCEPTION(ContactAdapter is null)
            databaseConnector.close();
        }
    }    
}

LogCat:

E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.NullPointerException
 at com.va.testappadr.AddressBook$GetContactsTask.onPostExecute(AddressBook.java:92)
at com.va.testappadr.AddressBook$GetContactsTask.onPostExecute(AddressBook.java:77)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)...
1
  • which line is line number 92? Commented Apr 10, 2016 at 8:04

1 Answer 1

1

The problem is that you are shadowing the cursor adapter by doing this:

CursorAdapter contactAdapter = new ...

in the onCreate method... hence the Async task is manipulating an object that was never initialized....

Why?

because you are declaring a new object in the onCreate ... use the global instead and initialize it...

in the onCreate change the code...

 // display contact name on TextView in ListView
        String[] from = new String[]{"name"};
        int[] to = new int[]{R.id.contactTextView};
        //HERE!!
        contactAdapter = new SimpleCursorAdapter(AddressBook.this, R.layout.contact_list_item, null, from, to);
        setListAdapter(contactAdapter); // setting adaptercontactView
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.