更新数据库中的数据时出现NullPointerException

时间:2013-02-02 12:49:14

标签: android sqlite nullpointerexception android-sqlite

该应用程序有2个活动,第1个活动有2个字段,第2个活动有1个字段。前两个字段插入表中,即第一个活动。一旦插入前两个字段,用户就会被重定向到新活动。新活动有第三个字段,保存时应在同一个表中更新。但是,在插入前两个值之后,当Iam更新同一个表中的第三个值时,我得到了nullpointer异常。提前感谢。

这是我第二次更新第3个值的活动

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        ed3=(EditText)findViewById(R.id.data3id);
        updatebtn=(Button)findViewById(R.id.updatebtnid);


        final Intent intent = getIntent();
        mRowId=intent.getStringExtra("KEYROWID");


        updatebtn.setOnClickListener(new OnClickListener(){


            public void onClick(View v) {
                dba = new DBAdapter(v.getContext());

                String data3=ed3.getText().toString();


                dba.open();


                 String updatequery = "UPDATE "+ DBAdapter.DATABASE_TABLE+ " SET ";

                updatequery = updatequery + "KEY_DATA3='" +data3 +"'";
                Log.v("updatequery",updatequery);
                updatequery = updatequery + " WHERE _id = '" + mRowId + "';";
                Log.v("updatequery-new",updatequery);
                Log.v("test-1","update");
                try{
                    Log.v("test-2","update");
                    myDB.execSQL(updatequery);

                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    Log.v("test-3","update");

                    if(myDB != null)myDB.close();

                }


            }

这是我的DBAdapter类

public class DBAdapter {
    public static final String KEY_ROWID= "_id";
    public static final String KEY_DATA1="data1";
    public static final String KEY_DATA2="data2";
    public static final String KEY_DATA3="data3";


    private static String TAG="DBAdapter";

    static String DATABASE_NAME="update.db";
    static final String DATABASE_TABLE="update_table";
    private static int DATABASE_VERSION=2;


    private final Context context;
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE =
      "create table update_table(_id integer primary key autoincrement, "
    + "data1 varchar, data2 varchar, data3 varchar);";




    private static class DatabaseHelper extends SQLiteOpenHelper {

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

        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }


        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS update_table");
            onCreate(db);
        }
    }




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


    public DBAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(context);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }


    public void close() {
        mDbHelper.close();
    }





    public long createddb(String data1,String data2,String data3) {

        ContentValues initialValues = new ContentValues();


        initialValues.put(KEY_DATA1, data1);
        initialValues.put(KEY_DATA2, data2);
        initialValues.put(KEY_DATA3, data3);


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




    }



    public boolean deleteSaleseditdetails(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }




    public int fetchmaxId(){
        //return mDb.query(DATABASE_TABLE, new String[]{KEY_ROWID},null, null, null, null, null);
        String selectQuery = "SELECT max("+KEY_ROWID+") from " + DATABASE_TABLE ;
         Cursor c= mDb.rawQuery(selectQuery, null);
        c.moveToFirst();
        return c.getInt(0);
        //  cursor.moveToLast();


    }



    public boolean deletedb(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

以下是我进入logcat文件的错误

02-02 18:10:48.751: W/System.err(17003): java.lang.NullPointerException
02-02 18:10:48.761: W/System.err(17003):    at com.example.testupdate.Activity2$1.onClick(Activity2.java:62)
02-02 18:10:48.761: W/System.err(17003):    at android.view.View.performClick(View.java:2408)
02-02 18:10:48.761: W/System.err(17003):    at android.view.View$PerformClick.run(View.java:8816)
02-02 18:10:48.761: W/System.err(17003):    at android.os.Handler.handleCallback(Handler.java:587)
02-02 18:10:48.761: W/System.err(17003):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 18:10:48.761: W/System.err(17003):    at android.os.Looper.loop(Looper.java:123)
02-02 18:10:48.761: W/System.err(17003):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-02 18:10:48.761: W/System.err(17003):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 18:10:48.761: W/System.err(17003):    at java.lang.reflect.Method.invoke(Method.java:521)
02-02 18:10:48.761: W/System.err(17003):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 18:10:48.761: W/System.err(17003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 18:10:48.761: W/System.err(17003):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

问题出在myDB字段null。我在您的代码中看不到,但请检查您是否在某处初始化了myDB字段。如果不是,那就是NullPointerException

顺便说一下,您正在检查myDB是否不是null,以便知道您是否应关闭它:

if(myDB != null)myDB.close();

但是,在致电myDB.execSQL(updatequery);

时,您没有进行同样的检查