插入SQLite数据库 - Android

时间:2013-01-02 22:30:02

标签: java android sqlite nullpointerexception

我正在尝试为highscores表实现SQLite数据库。我只是测试它,看看我的数据库创建和插入是否使用下面的代码。但是我在下面的行中得到了NullPointerException。

提前谢谢!

Results.java

public class Results extends Activity {
    DatabaseHelper dh;

    public void onCreate(Bundle savedInstanceState) {
        dh = DatabaseHelper.getInstance(this);

    public void showResults() {
        dh.openDB();  
        dh.insert(1231423, 436346);  //Line 104
    }
}

DatabaseHelper.java

private static final String SCORE = "score"; 
private static final String PERCENTAGE = "percentage";
public static DatabaseHelper mSingleton = null;

public synchronized static DatabaseHelper getInstance(Context context) {
    if(mSingleton == null) {
        mSingleton = new DatabaseHelper(context.getApplicationContext());
    }
    return mSingleton;
}

public SQLiteDatabase openDB() {
    return this.getWritableDatabase();
}

public long insert(long score, int percentage) {
    ContentValues values = new ContentValues();
    values.put(SCORE, score);  
    values.put(PERCENTAGE, percentage); 

    return db.insert(TABLE, null, values);  //Line 91
}

LogCat输出

01-02 17:56:47.609: E/AndroidRuntime(1322): FATAL EXCEPTION: main
01-02 17:56:47.609: E/AndroidRuntime(1322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.os.Looper.loop(Looper.java:137)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invoke(Method.java:511)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at dalvik.system.NativeStart.main(Native Method)
01-02 17:56:47.609: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:91)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.Results.showResults(Results.java:104)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.Results.onCreate(Results.java:50)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.Activity.performCreate(Activity.java:5008)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-02 17:56:47.609: E/AndroidRuntime(1322):     ... 11 more

编辑: NullPointerException至少会跳转,但错误仍然存​​在。我编辑了我的代码并发布了新的LogCat输出。

4 个答案:

答案 0 :(得分:1)

更改openDB()以保存它打开的SqliteDatabase并打开可写数据库:

public void openDB() {
    db = this.getWritableDatabase();
}

但是这仍然与您的堆栈跟踪不匹配,但它与您提供的代码匹配。

答案 1 :(得分:1)

你说你在做:

DatabaseHelper dh;

dh.insert(1231423, 436346);

看起来你没有实例化dh:

dh = new DatabaseHelper(this);

更好的方法是使用单例模式,因为您只需要一个DatabaseHelper实例:

private static final String TABLE_NAME = "table_name";
private static final int SCHEME_VERSION = 1;
public static DatabaseHelper mSingleton = null;

private DatabaseHelper(Context ctxt) {
    super(ctxt, DATABASE_NAME, null, SCHEME_VERSION);
}

public synchronized static DatabaseHelper getInstance(Context ctxt) {
    if (mSingleton == null)
        mSingleton = new DatabaseHelper(ctxt.getApplicationContext());
    return mSingleton;
}

现在您可以这样打电话:

DatabaseHelper dh = DatabaseHelper.getInstance(this);

答案 2 :(得分:0)

如果你遵循常见的SqlliteHelper模式,你可能忘记在调用insert之前调用.open()方法。

答案 3 :(得分:-1)

package com.example.hostelmangement;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

public DataBaseHelper(Context context, String name, CursorFactory factory,
        int version)
{
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db)
{
    db.execSQL("create table demo(id integer primary key,join_date date,name text,father_name text,date_of_birth text,house_name text,place text,pin_no text,blood_group text,mobile text,email text,company text,company_place text,company_phone text,company_pin,rent text,pending integer,status text,photo text,Proof text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    // TODO Auto-generated method stub

}

}