0

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

先感谢您!

结果.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
    }
}

数据库助手.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

4 回答 4

1

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

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

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

于 2013-01-02T23:08:08.853 回答
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);
于 2013-01-02T23:08:11.913 回答
0

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

于 2013-01-02T22:36:23.683 回答
-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

}

}

于 2015-10-14T03:51:03.707 回答