Question

I am trying to put the values of two columns created in the database to two text views of simple list item 2 using simple cursor adapter but I am getting NPE. Please help.
Regards

My code is:

class NotesList extends ListActivity{
    private DataSource datasource;
     ArrayAdapter<Message> adapter;
     Cursor cursors = null;
     private SQLiteDatabase database;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        datasource = new DataSource(this);
        datasource.open();


       // List<Message> values = datasource.getAllMessages();
    //    adapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_1, values);
    //     setListAdapter(adapter);


        String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+ " FROM " + MySQLiteHelper.TABLE_NAME ;
        cursors = database.rawQuery(queryz, null);

           ListAdapter adapter = new SimpleCursorAdapter(
                     this,
                     android.R.layout.simple_list_item_2,
                     cursors,     // Pass in the cursor to bind to.
                     new String[] {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_MESSAGE}, // Array of cursor columns to bind to.
                     new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.

             // Bind to our new adapter.
             setListAdapter(adapter);

    }
    @Override
    public void onResume() {
        datasource.open();

        super.onResume();
      }


      @Override
    public void onPause() {
        datasource.close();

        super.onPause();

      }

Datasource and Mysqlitehelper are the names of my classes.MySqliteHelper class creates the two columns of id and notes. Logcat is:

03-19 01:15:32.557: E/AndroidRuntime(10579): FATAL EXCEPTION: main
03-19 01:15:32.557: E/AndroidRuntime(10579): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.notes/soft.b.notes.NotesList}: java.lang.NullPointerException
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.os.Looper.loop(Looper.java:137)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.main(ActivityThread.java:4340)
Was it helpful?

Solution

Your database is not initialized.

Also, it's useful to examine the full stack trace. There's a "caused by" exception nested in the RuntimeException. Double-clicking a stacktrace row in your IDE would show exactly the line of code where the problem occurs.

OTHER TIPS

You're running a rawQuery on a database that's null. I don't see anywhere that you actually get a SQLiteDatabase for your database variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top