简体   繁体   English

Android - 为什么我在SQLiteOpenHelper onCreate中得到NullPointerException?

[英]Android - Why am I getting NullPointerException in SQLiteOpenHelper onCreate?

I have used LogCat to determine the problem line. 我使用LogCat来确定问题行。

The SQL String I am trying to execute in onCreate is.. 我试图在onCreate中执行的SQL字符串是..

CREATE TABLE Routines(_id integer primary key autoincrement, json TEXT);

When it tries to execute this, the problem occurs. 当它尝试执行此操作时,会出现问题。

This may well be whats causing the NullPointerException . 这可能是导致NullPointerException If you can't see anything wrong with that, please read on for a bit more background. 如果你看不出有任何问题,请继续阅读更多背景信息。

This database has not been created yet, I keep getting a NullpointerException , I have compared this to previous SQLite code and the problem is proving evasive. 这个数据库还没有创建,我一直得到一个NullpointerException ,我把它与以前的SQLite代码进行了比较,问题证明是回避的。

However, I mention this as it still has to go through the onCreate method. 但是,我提到这一点,因为它仍然必须通过onCreate方法。 I create a new DatabaseOpenHelper (extending SQliteOpenHelper) in my main code and call the helper's open() method as seen below. 我在主代码中创建了一个新的DatabaseOpenHelper (扩展SQliteOpenHelper)并调用helper的open()方法,如下所示。

public void open() throws SQLException
{
    ssDatabase = databaseOpenHelper.getWritableDatabase();
}

If I am not mistaken, as my database has not been created (I made sure of that through uninstalling it prior). 如果我没有弄错,因为我的数据库尚未创建(我之前通过卸载确认了这一点)。 The onCreate SQLiteHelper is invoked when that open() method is called. 调用open()方法时,将调用onCreate SQLiteHelper。

This is the code where I call that open method. 这是我称之为open方法的代码。

    try
    {                   
        dbConnector = new DatabaseConnector (this);
        debug = "2 ";

        // This on first start will invoke the database onCreate method - throws SQLException
        dbConnector.open();  /* PROBLEM LINE */
        debug = "3 ";
    }
    catch (Exception e)
    {
        textView1.setText(debug + e.toString());
    }

And this is the code containing my DatabaseOpenHelper and onCreate method 这是包含我的DatabaseOpenHelper和onCreate方法的代码

private class DatabaseOpenHelper extends SQLiteOpenHelper
{
    public DatabaseOpenHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, factory, version); 

        Log.i(TAG, "Constructor");
    }

    // On initial creation of database
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        Log.i(TAG, "Before SQL command");

        String sqlCreateCommand = "CREATE TABLE Routines"
                + "(_id integer primary key autoincrement, "
                + "json TEXT);";

        Log.i(TAG, sqlCreateCommand); 

        // I believe this to be the PROBLEM LINE
        ssDatabase.execSQL(sqlCreateCommand); 

        Log.i(TAG, "Jab done");
    }

    // On upgrade - currently do nothing
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {}      
}   

Thanks for the help! 谢谢您的帮助!

seems like i dont have the prvilege to just post a comment. 好像我没有发表评论的吝啬。 One question: ssDatabase.execSQL(sqlCreateCommand); 一个问题: ssDatabase.execSQL(sqlCreateCommand); --> shouldn't this be db.execSQL(sqlCreateCommand); - >不应该是db.execSQL(sqlCreateCommand); since the parameter is SQLiteDatabase db. 因为参数是SQLiteDatabase db。 Secondly you use _id. 其次你使用_id。 is this index not created by default ? 这个索引是不是默认创建的? Nevermind if this has nothing to do with it. 没关系,如果这与它无关。 This were just my thoghts about it. 这只是我对它的打击。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM