Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:33:42+00:00 2026-05-28T21:33:42+00:00

Why does this code crash? CustomDatabaseHelper.java: public class CustomDatabaseHelper { SQLiteDatabase db; private static

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:33:43+00:00Added an answer on May 28, 2026 at 9:33 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code: class A { public: explicit A(int x) {} }; class
Why does this code not print an exception stack trace? public class Playground {
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>
Why does this code crash the program when I run it ostream& operator<<(ostream& cout,
Why does this code crash the Scala 2.8.1 compiler? val a = new Array[{
Using NSXMLParser why does this line of code cause an exception: ushort crash =
Why does this code crash my app onte emulator and on the device -
Why does this pdo::mysql code crash on windows??? <?php $username = root; $password =
Does this code cause a memory leak: int main(){ int * a = new
Why does this code fail to display the category name Apples using the current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.