Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

database - Android NullPointerException + GetDatabaseLocked

I have got a problem with my programm. I use a database to save the settings of my app. When I save the password in the app it′s all ok, but when I save the name of a "school class" with in principle the same method, but another table it crashes and I have got this error:

02-19 15:32:04.070: E/AndroidRuntime(14349): FATAL EXCEPTION: main
02-19 15:32:04.070: E/AndroidRuntime(14349): java.lang.NullPointerException
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at de.vertretungsplan2.helper.DBHandler.insertKurs(DBHandler.java:95)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at de.vertretungsplan2.tabswipe.adapter.KurseFragment$1.onItemClick(KurseFragment.java:38)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.widget.AdapterView.performItemClick(AdapterView.java:297)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.widget.AbsListView$1.run(AbsListView.java:3463)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.os.Handler.handleCallback(Handler.java:730)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.os.Looper.loop(Looper.java:137)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at android.app.ActivityThread.main(ActivityThread.java:5289)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at java.lang.reflect.Method.invokeNative(Native Method)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at java.lang.reflect.Method.invoke(Method.java:525)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
02-19 15:32:04.070: E/AndroidRuntime(14349):    at dalvik.system.NativeStart.main(Native Method)

This is the Method that should write in the database:

public void insertKurs(String Kurs){
    long rowId = -1;
    try{
        //open Database
        SQLiteDatabase db = this.getWritableDatabase();
        //Content
        ContentValues values = new ContentValues();
        values.put(KURS_NAME, Kurs);
        //insert Values in Table 
        rowId = db.insert(TABLE_NAME_KURS, null, values);
        db.close();
    }catch (SQLiteException e){
        Log.e(TAG,"insert()",e);
    }finally{
        Log.d(TAG, "insert(): rowId=" + rowId);         
    }
}

This Method work without any Problems:

public void insertPW(String PW){
    SQLiteDatabase db = null;
    long rowId = -1;
    try{
        //open Database
        db = this.getWritableDatabase();
        //Content
        ContentValues values = new ContentValues();
        values.put(PASSWORD_NAME, PW);
        //insert Values in Table 
        rowId = db.insert(TABLE_NAME_PW, null, values);
        db.close();
    }catch (SQLiteException e){
        Log.e(TAG,"insert()",e);
    }finally{
        Log.d(TAG, "insert(): rowId=" + rowId);         
    }
}

This is the method that call the other:

public void onItemClick(AdapterView<?>arg0,View v, int position,long id){                   
            db.insertKurs(kursView1.getItemAtPosition(position).toString());

        }
    }); 

Thank you for the help and sorry for my english :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Context you passed to the SQLiteOpenHelper constructor was null.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...