开发者

Error when creating entry into SQLdatabase

开发者 https://www.devze.com 2023-03-21 03:40 出处:网络
I keep getting this error 07-19 14:24:50.840: ERROR/AndroidRuntime(1512): java.lang.NullPointerException

I keep getting this error

07-19 14:24:50.840: ERROR/AndroidRuntime(1512): java.lang.NullPointerException
07-19 14:24:50.840: ERROR/AndroidRuntime(1512):     at com.fttech.organizeit.TaskHelper.createLunch(TaskHelper.java:76)


07-19 14:24:50.840: ERROR/AndroidRuntime(1512):     at com.fttech.organizeit.TaskDetails.saveState(TaskDetails.java:260)

Here is my createLunch()

}
    public lo开发者_如何转开发ng createTask(String title, String descrip, String type, String taskdatetime){
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_DESCRIP, descrip);
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_DATE_TIME, taskdatetime);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

It gives me a error at createLunch before saveState so im pretty sure the error lies here. Its pointing me to my return statement. i just dont see what the problem is.

Im seeing the lob.v for saved here. but not for inserted into database. i think the error lies here

Log.v(title, "saved");

    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
    String taskdatetime = dateTimeFormat.format(mCalendar.getTime());
    if(taskId == null){
        long id = helper.createTask(title, descrip, type, taskdatetime);
        Log.v(taskdatetime, "Inserted into databse");
        if(id >0){
            taskId = id;
        }
    }
    else{
        helper.updateTask(taskId, title, descrip, type, taskdatetime);
    }

    finish();


}

}

public void fillData(){
            helper.open();
            Cursor task = helper.fetchAllTask();
            startManagingCursor(task);

            String[] from = new String[]{TaskHelper.KEY_TITLE};
            int[] to = new int[]{R.layout.row};

            SimpleCursorAdapter taskAdapter = new SimpleCursorAdapter(this, R.layout.list, task, from , to);
            setListAdapter(taskAdapter);

        }


Is your mDB null? Try this and see if your id is returning as -1

public long createTask(String title, String descrip, String type, String taskdatetime){
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_DESCRIP, descrip);
    initialValues.put(KEY_TYPE, type);
    initialValues.put(KEY_DATE_TIME, taskdatetime);

    long lResult=-1;
    if(mDB!=null)
    lResult = mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    return lResult;
}

UPDATE

You should open your database by calling the getWriteableDatabase method of your DatabaseHelper class which should be extend SQLiteOpenHelper.

mDB = myDataBaseHelper.getWritableDatabase()

You should be checking the result of your call to createTask

   if(id >0){
        Log.v(taskdatetime, "Inserted into databse");
        taskId = id;
   }
   else
   {
        Log.v(taskdatetime, "Insert failed");
   }


Yoshi,

From the code the only thing that jumps out is the mDB object. Did you declare it somewhere else in the class. If not then that's your problem. If so what is line 76. Could you post that.

In case you need it. Here's a snippet of code declaring the Mdb:

private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper (Context context) {
            super(context, DBConstants.DATABASE_NAME, null, DBConstants.DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            //db.execSQL(DBConstants.TABLE_CREATE);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVerison)
        {
            //db.execSQL(DATABASE_UPGRADE);

        }
    }

//Open the data base.  It will either create or upgrade the database
public DbAdpt open() throws android.database.SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

Hope this helps, George

0

精彩评论

暂无评论...
验证码 换一张
取 消