{"id":1836,"date":"2022-08-30T15:19:51","date_gmt":"2022-08-30T15:19:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/i-cant-reading-sqlite-database-by-cursor-it-is-always-crashing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:51","modified_gmt":"2022-08-30T15:19:51","slug":"i-cant-reading-sqlite-database-by-cursor-it-is-always-crashing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/i-cant-reading-sqlite-database-by-cursor-it-is-always-crashing-collection-of-common-programming-errors\/","title":{"rendered":"I can&#39;t reading SQLite Database by cursor. It is always crashing-Collection of common programming errors"},"content":{"rendered":"<p>Why does this code crash?<\/p>\n<p>CustomDatabaseHelper.java:<\/p>\n<pre><code>public class CustomDatabaseHelper {\n\nSQLiteDatabase db;\nprivate static final String DATABASE_TABLE = \"tbl_homework\";\npublic static final String KEY_TITLE = \"hw\";\npublic static final String KEY_ROWID = \"id\";\nprivate final Context mCtx;\n\npublic Cursor executeSQLQuery(String query){\n    Cursor c = db.rawQuery(query,null);\n    return c;\n}\n\npublic Cursor fetchAllNotes() {\n\n    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE}, null, null, null, null, null);\n}\n\npublic CustomDatabaseHelper(Context ctx) {\n    this.mCtx = ctx;\n}    \n}\n<\/code><\/pre>\n<p>homework.java:<\/p>\n<pre><code>public class homework extends ListActivity {\nSQLiteDatabase db;\nprivate CustomDatabaseHelper mDbHelper;\n\n\/** Called when the activity is first created. *\/\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.homework);\n\n    \/\/Create Database\n    db = openOrCreateDatabase(\"test_database.db\", \n            SQLiteDatabase.CREATE_IF_NECESSARY, null);\n\n    db.setVersion(1);\n    db.setLocale(Locale.getDefault());\n    db.setLockingEnabled(true);\n\n    \/\/Create table tbl_homework\n    final String CREATE_TABLE_HW =\n            \"CREATE TABLE IF NOT EXISTS tbl_homework (\"\n            + \"id INTEGER PRIMARY KEY AUTOINCREMENT,\"\n            + \"hw TEXT);\";\n\n            db.execSQL(CREATE_TABLE_HW);\n\n   press_cmd_back();\n   press_cmd_test_data();\n\n   mDbHelper = new CustomDatabaseHelper(this);\n\n   fillData();\n}\n\n\/\/What will happen if cmd_back gets pressed\nprivate void press_cmd_back(){\n    Button cmd_back = (Button)findViewById(R.id.cmd_back);\n    cmd_back.setOnClickListener(new View.OnClickListener() {\n\n        public void onClick(View v) {\n            Intent Intent_main = new Intent(homework.this, Noten_HausaufgabenActivity.class);\n            startActivity(Intent_main);\n        }\n    });\n}\n\n\/\/What will happen if cmd_test_data gets pressed\nprivate void press_cmd_test_data(){\n    Button cmd_test_data = (Button)findViewById(R.id.cmd_eintragen);\n    cmd_test_data.setOnClickListener(new View.OnClickListener() {\n\n        public void onClick(View v) {\n            open_database_rw();\n            EditText txt_hw = (EditText)findViewById(R.id.edt_eingabe);\n            String txt_insert_hw = txt_hw.getText().toString(); \n\n            final String INSERT_HW = \"INSERT INTO tbl_homework ('hw') VALUES ('\" + txt_insert_hw + \"')\";\n\n            db.execSQL(INSERT_HW);\n\n        }\n    });\n\n}\n\n\/\/Open Database RW\nprivate void open_database_rw() {\n    db = SQLiteDatabase.openDatabase(\"\/data\/data\/test.marco.notenha\/databases\/test_database.db\", \n            null, SQLiteDatabase.OPEN_READWRITE);\n}\n\nprivate void fillData() {\n    \/\/ Get all of the notes from the database and create the item list\n\n    Cursor c = mDbHelper.fetchAllNotes();\n\n    \/*startManagingCursor(c);\n\n    String[] from = new String[] { CustomDatabaseHelper.KEY_TITLE };\n    int[] to = new int[] {R.id.txt_notes_row};\n\n    \/\/ Now create an array adapter and set it to display using our row\n    SimpleCursorAdapter notes =\n        new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);\n        setListAdapter(notes);*\/\n}\n\n}\n\nError log:\n\n01-25 17:20:30.152: D\/dalvikvm(7421): GC_EXTERNAL_ALLOC freed 48K, 50% free 2690K\/5379K, external 0K\/0K, paused 70ms\n01-25 17:20:30.835: D\/dalvikvm(7421): GC_EXTERNAL_ALLOC freed 32K, 49% free 2757K\/5379K, external 203K\/523K, paused 19ms\n01-25 17:20:30.871: D\/AndroidRuntime(7421): Shutting down VM\n01-25 17:20:30.871: W\/dalvikvm(7421): threadid=1: thread exiting with uncaught exception (group=0x4006f568)\n01-25 17:20:30.871: E\/AndroidRuntime(7421): FATAL EXCEPTION: main\n01-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\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.os.Handler.dispatchMessage(Handler.java:99)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.os.Looper.loop(Looper.java:130)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.ActivityThread.main(ActivityThread.java:3703)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at java.lang.reflect.Method.invokeNative(Native Method)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at java.lang.reflect.Method.invoke(Method.java:507)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at dalvik.system.NativeStart.main(Native Method)\n01-25 17:20:30.871: E\/AndroidRuntime(7421): Caused by: java.lang.NullPointerException\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at test.marco.notenha.CustomDatabaseHelper.fetchAllNotes(CustomDatabaseHelper.java:22)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at test.marco.notenha.homework.fillData(homework.java:89)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at test.marco.notenha.homework.onCreate(homework.java:46)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)\n01-25 17:20:30.871: E\/AndroidRuntime(7421):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)\n<\/code><\/pre>\n<ol>\n<li>\n<p>The app is crashing because of a null pointer in <code>fetchAllNotes<\/code>. This is happening because you aren&#8217;t initializing <code>db<\/code> to anything inside <code>CustomDatabaseHelper<\/code>. You can try adding this line:<\/p>\n<pre><code>mDbHelper.db = db;\n<\/code><\/pre>\n<p>just before the call to <code>fillData()<\/code> in <code>onCreate()<\/code>. 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 <code>SQLiteOpenHelper<\/code> class for a better way to do all this. (Also read the &#8220;Using Databases&#8221; section of the Data Storage guide topic.)<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 20:57:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Why does this code crash? CustomDatabaseHelper.java: public class CustomDatabaseHelper { SQLiteDatabase db; private static final String DATABASE_TABLE = &#8220;tbl_homework&#8221;; public static final String KEY_TITLE = &#8220;hw&#8221;; public static final String KEY_ROWID = &#8220;id&#8221;; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1836","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1836"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1836\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}