获取可写数据库时Android中的NullPointerException

时间:2013-07-15 15:31:24

标签: android database sqlite nullpointerexception

我在android中制作一个简单的应用程序。我无法运行程序,logcat显示此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.tasks/com.sample.tasks.ToDo}: java.lang.NullPointerException

在考试之后,在这个陈述中看起来像是他们的问题:

db = DBHelper.getWritableDatabase();

或本声明:

final String DATABASE_CREATE = "CREATE TABLE tasks(sno INTEGER PRIMARY KEY ASC, todo TEXT);";
    db.execSQL(DATABASE_CREATE);

以下是完整的代码:

    public class DBAdapter {

    public static final String KEY_ROWID = "SNo";
    public static final String KEY_TASKS =  "Tasks";


    private static final String DATABASE_NAME = "Records";
    private static final String DATABASE_TABLE = "Tasks";
    private static final int DATABASE_VERSION = 1;

    private final Context context;
    private DbHelper DBHelper;
    private static SQLiteDatabase db;

    public DBAdapter(Context ctx)
    { 
    this.context = ctx;
    DBHelper = new DbHelper(context);
    }

    private static class DbHelper extends SQLiteOpenHelper
    {
    DbHelper(Context context)
    {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        db.execSQL("Create Table " + DATABASE_TABLE + "(" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_TASKS + " TEXT )");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE );
                System.out.println("Updating the Database");
                onCreate(db);
    }

}
    public DBAdapter open() throws SQLException
    {
    db = DBHelper.getWritableDatabase();
    return this;
    }

    public void close()
    {
    DBHelper.close();
    System.out.println("In close");
    }

    public long createEntry(String task){
        .....
    }

    public String[] getData() {
        .....
    }

}

我试过寻找未初始化的对象(因此为null)但找不到任何东西。请帮助我,我错过了什么?

更新

以下是其他文件的主要部分:

public class ToDo extends Activity implements OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.to_do);


        String[] tasks = new String[16];

        //Retrive tasks from database and view it on the screen
        DBAdapter info = new DBAdapter(this);
        info.open();
        tasks = info.getData();
        info.close();
    }
}

这里是完整的错误消息:

07-15 16:11:19.187: E/AndroidRuntime(914): FATAL EXCEPTION: main
07-15 16:11:19.187: E/AndroidRuntime(914): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.tasks/com.sample.tasks.ToDo}: java.lang.NullPointerException
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.os.Looper.loop(Looper.java:123)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 16:11:19.187: E/AndroidRuntime(914):  at java.lang.reflect.Method.invokeNative(Native Method)
07-15 16:11:19.187: E/AndroidRuntime(914):  at java.lang.reflect.Method.invoke(Method.java:507)
07-15 16:11:19.187: E/AndroidRuntime(914):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 16:11:19.187: E/AndroidRuntime(914):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 16:11:19.187: E/AndroidRuntime(914):  at dalvik.system.NativeStart.main(Native Method)
07-15 16:11:19.187: E/AndroidRuntime(914): Caused by: java.lang.NullPointerException
07-15 16:11:19.187: E/AndroidRuntime(914):  at com.sample.tasks.DBAdapter$DbHelper.onCreate(DBAdapter.java:46)
07-15 16:11:19.187: E/AndroidRuntime(914):  at com.sample.tasks.DBAdapter.open(DBAdapter.java:60)
07-15 16:11:19.187: E/AndroidRuntime(914):  at com.sample.tasks.ToDo.onCreate(ToDo.java:59)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-15 16:11:19.187: E/AndroidRuntime(914):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

2 个答案:

答案 0 :(得分:1)

在评论中回答njzk2给出的问题,

在DBAdapter类的onCreate()方法中,将参数从arg0更改为db。您当前正在对未初始化的另一个对象执行查询。

答案 1 :(得分:0)

db.execSQL方法中调用onCreate函数之前,请致电 db = openOrCreateDatabase("MyDBPath", MODE_PRIVATE, null);。如果数据库尚不存在,则此方法将创建数据库;如果数据库存在,则将其打开。

有关该功能的完整说明,请参阅documentation