getWritableDatabase 抛出 NullPointerException [英] getWritableDatabase throws NullPointerException

查看:67
本文介绍了getWritableDatabase 抛出 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了现有的 NPE/数据库线程,但找不到任何直接相关的内容......

I've trawled through existing NPE/database threads but couldn't find anything directly relevant...

我有一个 OnClickListener 类(与关联的 Activity 类分开),它在下面的代码中调用 createFromPreset 方法.下面方法代码中的最后一行 (this.getWritableDatabase) 抛出一个 NPE .当我调试代码并在该行中断时,this"显示为TableControllerMealItem"(不是我期望的空值).

I have an OnClickListener class (separate from the associated Activity class) which calls the createFromPreset method in the code below. The last line in the method code below (this.getWritableDatabase) throws a NPE . When I debug the code and break at that line "this" is showing as "TableControllerMealItem" (not null as I was expecting).

此代码以前运行良好.这可能是巧合,但我刚刚创建了一个 Application 类(并在 Android Manifest 中注册了它).这会不会有什么问题??

This code was previously working fine. It could be a coincidence, but I just created an Application class (and registed it in the Android Manifest). Could this be messing with anything??

public class TableControllerMealItem extends MySQLiteHelper {
    public TableControllerMealItem(Context context) {
        super(context);
    }
public long createFromPreset(meal_item meal_item, long meal_id, long preset_item_id) {
        String presetDesc = "";
        int presetMinutes = 0;

        //Get details from the preset_item (description and minutes)
        String sql = "SELECT * FROM preset_item WHERE _id = " + preset_item_id;
        SQLiteDatabase db = this.getWritableDatabase();

这是 LogCat:

05-27 11:58:39.613: E/AndroidRuntime(3021): java.lang.NullPointerException
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at com.ian.mealtimer.TableControllerMealItem.createFromPreset(TableControllerMealItem.java:56)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at com.ian.mealtimer.OnClickListenerSelectPresetItem.onClick(OnClickListenerSelectPresetItem.java:40)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.view.View.performClick(View.java:4438)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.view.View$PerformClick.run(View.java:18422)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.os.Handler.handleCallback(Handler.java:733)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.os.Looper.loop(Looper.java:136)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at java.lang.reflect.Method.invoke(Method.java:515)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-27 11:58:39.613: E/AndroidRuntime(3021):     at dalvik.system.NativeStart.main(Native Method)

MySQLite 助手类在这里:

The MySQLite helper class is here:

public class MySQLiteHelper extends SQLiteOpenHelper {
    private static final String TAG = "MySQLiteHelper";
    public static final String TABLE_PRESET_ITEM = "preset_item";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRESET_DESC = "preset_desc";
    public static final String COLUMN_PRESET_MINUTES = "preset_minutes";
    protected static final String DATABASE_NAME = "mealtimersqlite.db";
    private static final int DATABASE_VERSION = 1;

    public MySQLiteHelper(Context context) {
        super(context, "mealtimersqlite.db", null, 5);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String SQLCommand;

        SQLCommand = "CREATE TABLE preset_item( _id INTEGER PRIMARY KEY, "
                + "preset_desc TEXT NULL, preset_minutes INTEGER NULL );";
        db.execSQL(SQLCommand);

        SQLCommand = "CREATE TABLE meal(_id INTEGER PRIMARY KEY,"
                + "meal_desc TEXT NULL, meal_ready_time INTEGER NULL, meal_reminders_flag INTEGER NULL);";
        db.execSQL(SQLCommand);

        SQLCommand = "CREATE TABLE meal_item(_id INTEGER PRIMARY KEY, "
                + "meal_item_desc TEXT NULL, meal_item_minutes INTEGER NULL, meal_item_start_time INTEGER NULL, "
                + "meal_item_ready_time INTEGER NULL, fk_meal_id INTEGER NOT NULL, fk_preset_item_id INTEGER NULL );";
        db.execSQL(SQLCommand);
        Log.d(TAG, "Mealtimer Database Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS preset_item;");
        db.execSQL("DROP TABLE IF EXISTS meal;");
        db.execSQL("DROP TABLE IF EXISTS meal_item;");
        Log.d(TAG, "Mealtimer Database Tables Dropped;");
        // Create tables again
        onCreate(db);
    }

}

这是调用 createFromPreset 方法的类:

Here's the class that calls the createFromPreset method:

public class OnClickListenerSelectPresetItem implements OnClickListener {
    public final static String EXTRA_MEAL_ID = "com.ian.mealtimer.MEAL_ID";
    private long glbMealId = 1;
    Context context;
    private Activity activity;

    public OnClickListenerSelectPresetItem(Activity activity){
        this.activity = activity;
        this.glbMealId = ((MealTimerApplication) activity.getApplication()).getMealId();
        }
    public void onCreate(long meal_id) {
    }

    public void onClick(View view) {
        //INSERT a meal_item using then preset_item_id of clicked item
        //then return to the MealItemDetail Activity

        //First get the id of the selected item
        Long selectedPresetItem = Long.valueOf(view.getTag().toString());

        //Now insert the meal_item record

        // Save new meal_item record
        meal_item meal_item = new meal_item();
        long newRecord = new TableControllerMealItem(
                context).createFromPreset(meal_item, glbMealId, selectedPresetItem);

推荐答案

您传递给 SQLiteOpenHelper 构造函数的 Contextnull.

The Context that you are passing to the SQLiteOpenHelper constructor is null.

这篇关于getWritableDatabase 抛出 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆