質問

I have been having this problem for a few days now, and really cant see a problem in the code, so a few extra eyes to have a look is really appreciated.

I start the app by simply creating a new database manager object, and opening the managers database.

 DBManager db_manager = new DBManager(this);
 db_manager.open()

App should then perform a simple insert transaction, but an error is thrown:

02-11 18:13:30.350: E/AndroidRuntime(8961): Caused by: java.lang.NullPointerException
02-11 18:13:30.350: E/AndroidRuntime(8961):     at com.test.DB.DBManager$DatabaseHelper.onCreate(DBManager.java:44)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at com.test.DB.DBManager.open(DBManager.java:61)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at com.test.DB.Display.onCreate(Display.java:26)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
02-11 18:13:30.350: E/AndroidRuntime(8961):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)

The DatabaseHelper class:

    class DatabaseHelper extends SQLiteOpenHelper{

    DatabaseHelper(Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){            
            the_db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
}

The DBManager methods, open() is causing the problem.

    public DBManager(Context c){
    this.context = c;
    helper = new DatabaseHelper(context); 
}


// Makes the_db available to the Manager class.
public DBManager open() throws SQLException{
    the_db = helper.getWritableDatabase();
    return this;
}
役に立ちましたか?

解決

Try :

    @Override
    public void onCreate(SQLiteDatabase db){            
            db.execSQL(DATABASE_CREATE);
    }

You already get the database in your onCreate() method, is the SQLiteDatabase parameter db and you must use it.

EDIT : You get the NullPointerException because you call executeSQL() on the_db which is a field in DBManager, probably declared like:

SQLiteDatabase the_db;

or

SQliteDatabase the_db = null;

so when you actually get in the onCreate(SQLiteDatabase db) you call executeSQL() on null. The code for DBManager should be something like this:

public class DBManager {

    //the actual database you will use to insert,select etc after the getWritableDatabase() call
    private SQLiteDatabase the_db; 
    private Context context;
    private DatabaseHelper helper;

    public DBManager(Context c) {
        this.context = c;
        helper = new DatabaseHelper(context);
    }

    // Makes the_db available to the Manager class.
    public DBManager open() throws SQLException {
        the_db = helper.getWritableDatabase();
        return this;
    }

    class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, "something.db", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // the db is the SQLiteDatabase that android will offer you
            // and you must use it!
            db.execSQL("CREATE TABLE nametables(name TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top