0

I keep on getting null pointer exception while updating the table on this line-59 i.e

myDB.execSQL(updatequery);

Note: In Activity 1 the constructor has 3 edit-text fields where the 1st 2 fields has the values and 3rd one is kept as null.As soon as 2nd activity starts the 3rd field is updated in the same table.

I am inserting data in activity-1 like this

dba.createddb(data11, data22, null);

Following is my activity where I am updating the data

public class Activity2 extends Activity {

    EditText ed3;
    Button btn3;
     SQLiteDatabase myDB;
    DBAdapter dba;
    private String mRowId;
    String data3;
    String updatequery;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        ed3=(EditText)findViewById(R.id.data3id);
        btn3=(Button)findViewById(R.id.updatebtnid);
        //dba=new DBAdapter(Activity2.this);
        final Intent intent = getIntent();
        mRowId=intent.getStringExtra("KEYROWID");


        btn3.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {


                dba = new DBAdapter(v.getContext());
                data3=ed3.getText().toString();
                updatequery  = "UPDATE "+ DBAdapter.DATABASE_TABLE+ " SET ";

                updatequery = updatequery + DBAdapter.KEY_DATA3+" = '" + data3 +"'";
                Log.v("updatequery",updatequery);
                updatequery = updatequery + " WHERE _id = '" + mRowId + "';";
                Log.v("updatequery-new",updatequery);
                Log.v("test-1","update");
                try{
                    Toast.makeText(v.getContext(), "UPDATING", Toast.LENGTH_SHORT).show();
                    Log.v("test-2","update");
                    dba.open();
                    myDB.execSQL(updatequery);
                        }catch(NullPointerException e){
                   //e.getCause();
                    e.getMessage();
                }finally{
                    Log.v("test-3","update");
                        if(myDB != null)myDB.close();
                        dba.close();
                    }
                }







        });

    }

The database which I am using:

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;
        open();
    }


    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(){

        String selectQuery = "SELECT max("+KEY_ROWID+") from " + DATABASE_TABLE ;
         Cursor c= mDb.rawQuery(selectQuery, null);
        c.moveToFirst();
        return c.getInt(0);



    }








    public boolean deletedb(long rowId) {

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



}

Following is myLogcat file:

02-04 11:53:20.216: E/AndroidRuntime(11235): FATAL EXCEPTION: main
02-04 11:53:20.216: E/AndroidRuntime(11235): java.lang.NullPointerException
02-04 11:53:20.216: E/AndroidRuntime(11235):    at com.example.testupdate.Activity2$1.onClick(Activity2.java:59)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at android.view.View.performClick(View.java:2408)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at android.view.View$PerformClick.run(View.java:8816)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at android.os.Handler.handleCallback(Handler.java:587)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at android.os.Looper.loop(Looper.java:123)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-04 11:53:20.216: E/AndroidRuntime(11235):    at dalvik.system.NativeStart.main(Native Method)
3
  • What is line number Activity2.java:59? Commented Feb 4, 2013 at 6:35
  • @shreya line no.59 is myDB.execSQL(updatequery); in the class(Activity2) Commented Feb 4, 2013 at 6:37
  • 1
    @Shweta : means you forget to initialize myDB instance before using it Commented Feb 4, 2013 at 6:39

3 Answers 3

2

And from your code, I found, myDB is NULL.

As you declared SQLiteDatabase myDB;

But forgot to initialize it. So this code line myDB.execSQL(updatequery); Gives you NullPointerException.

Update: (Only pseudo code for your understanding)

Actually in your code there is no need of SQLiteDatabase myDB;

You have to just make a method executeQuery(String query) in Class DBAdapter. in That just write

mDb.execSQL(query);

Now from your Activity instead of myDB.execSQL(updatequery); call dba.executeQuery(updatequery);

Sign up to request clarification or add additional context in comments.

Comments

1

You have not initialize the SQLiteDatabase myDB; in your activity and you are trying to use it directly that is the reason your application is throwing nullpointer exception in your onClick.

Just initialize the SQLiteDatabase myDB; in your onCreate() and then try to use it as below.

Add the context in your activity as :

    private Context context;
     public class Activity2 extends Activity {
        context=Activity2.this;
         SQLiteDatabase myDB=new ....();
          DBAdapter dba=new DBAdapter(context);
       }

I hope it will help you

Thanks.

3 Comments

as per your suggestion i initialized final SQLiteDatabase myDB=null under onCreate..but getting the same error
@Grishu - As she already has SQLiteDatabase mDb object in DBAdapter class so there is no need of re-initialize SQLiteDatabase myDB; in Activity2.
Don't initialize it as null dear. I mean to say SQLiteDatabase myDB=conext.openOrCreateDatabase(); .you need to open or create your database as you are writing it in your database adapter class.
0

I think it cause of Context.So can u use this code.

dba = new DBAdapter(Activity2.this);

instead of

dba = new DBAdapter(v.getContext());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.