I can't reading SQLite Database by cursor. It is always crashing-Collection of common programming errors

Why does this code crash?

CustomDatabaseHelper.java:

public class CustomDatabaseHelper {

SQLiteDatabase db;
private static final String DATABASE_TABLE = "tbl_homework";
public static final String KEY_TITLE = "hw";
public static final String KEY_ROWID = "id";
private final Context mCtx;

public Cursor executeSQLQuery(String query){
    Cursor c = db.rawQuery(query,null);
    return c;
}

public Cursor fetchAllNotes() {

    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
}

public CustomDatabaseHelper(Context ctx) {
    this.mCtx = ctx;
}    
}

homework.java:

public class homework extends ListActivity {
SQLiteDatabase db;
private CustomDatabaseHelper mDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homework);

    //Create Database
    db = openOrCreateDatabase("test_database.db", 
            SQLiteDatabase.CREATE_IF_NECESSARY, null);

    db.setVersion(1);
    db.setLocale(Locale.getDefault());
    db.setLockingEnabled(true);

    //Create table tbl_homework
    final String CREATE_TABLE_HW =
            "CREATE TABLE IF NOT EXISTS tbl_homework ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "hw TEXT);";

            db.execSQL(CREATE_TABLE_HW);

   press_cmd_back();
   press_cmd_test_data();

   mDbHelper = new CustomDatabaseHelper(this);

   fillData();
}

//What will happen if cmd_back gets pressed
private void press_cmd_back(){
    Button cmd_back = (Button)findViewById(R.id.cmd_back);
    cmd_back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent Intent_main = new Intent(homework.this, Noten_HausaufgabenActivity.class);
            startActivity(Intent_main);
        }
    });
}

//What will happen if cmd_test_data gets pressed
private void press_cmd_test_data(){
    Button cmd_test_data = (Button)findViewById(R.id.cmd_eintragen);
    cmd_test_data.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            open_database_rw();
            EditText txt_hw = (EditText)findViewById(R.id.edt_eingabe);
            String txt_insert_hw = txt_hw.getText().toString(); 

            final String INSERT_HW = "INSERT INTO tbl_homework ('hw') VALUES ('" + txt_insert_hw + "')";

            db.execSQL(INSERT_HW);

        }
    });

}

//Open Database RW
private void open_database_rw() {
    db = SQLiteDatabase.openDatabase("/data/data/test.marco.notenha/databases/test_database.db", 
            null, SQLiteDatabase.OPEN_READWRITE);
}

private void fillData() {
    // Get all of the notes from the database and create the item list

    Cursor c = mDbHelper.fetchAllNotes();

    /*startManagingCursor(c);

    String[] from = new String[] { CustomDatabaseHelper.KEY_TITLE };
    int[] to = new int[] {R.id.txt_notes_row};

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);*/
}

}

Error log:

01-25 17:20:30.152: D/dalvikvm(7421): GC_EXTERNAL_ALLOC freed 48K, 50% free 2690K/5379K, external 0K/0K, paused 70ms
01-25 17:20:30.835: D/dalvikvm(7421): GC_EXTERNAL_ALLOC freed 32K, 49% free 2757K/5379K, external 203K/523K, paused 19ms
01-25 17:20:30.871: D/AndroidRuntime(7421): Shutting down VM
01-25 17:20:30.871: W/dalvikvm(7421): threadid=1: thread exiting with uncaught exception (group=0x4006f568)
01-25 17:20:30.871: E/AndroidRuntime(7421): FATAL EXCEPTION: main
01-25 17:20:30.871: E/AndroidRuntime(7421): java.lang.RuntimeException: Unable to start activity ComponentInfo{test.marco.notenha/test.marco.notenha.homework}: java.lang.NullPointerException
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.os.Looper.loop(Looper.java:130)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.ActivityThread.main(ActivityThread.java:3703)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at java.lang.reflect.Method.invoke(Method.java:507)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at dalvik.system.NativeStart.main(Native Method)
01-25 17:20:30.871: E/AndroidRuntime(7421): Caused by: java.lang.NullPointerException
01-25 17:20:30.871: E/AndroidRuntime(7421):     at test.marco.notenha.CustomDatabaseHelper.fetchAllNotes(CustomDatabaseHelper.java:22)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at test.marco.notenha.homework.fillData(homework.java:89)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at test.marco.notenha.homework.onCreate(homework.java:46)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-25 17:20:30.871: E/AndroidRuntime(7421):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
  1. The app is crashing because of a null pointer in fetchAllNotes. This is happening because you aren’t initializing db to anything inside CustomDatabaseHelper. You can try adding this line:

    mDbHelper.db = db;
    

    just before the call to fillData() in onCreate(). That should solve the null pointer exception. However, I think your code will have other problems if the data base already exists. You might want to take a look at the SQLiteOpenHelper class for a better way to do all this. (Also read the “Using Databases” section of the Data Storage guide topic.)