Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:38:07+00:00 2026-06-18T05:38:07+00:00

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

  • 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:38:08+00:00Added an answer on June 18, 2026 at 5:38 am

    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);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this error java.null.pointer exception When ever i trie to run this
so i am using the Processing IDE and keep getting this strange null pointer
I keep getting this java.lang.NullPointerException on the line where mLoginButton's onClickListener is initiated. I
I keep getting a null exception at the ; below. The ApiUsername & ApiPassword
I keep getting null pointer exceptions for methods of MediaPlayer. I was finally able
I'm trying to access servletContextin a controller like so, but keep getting null pointer
I keep getting Activity Monitor Job. java.lang.NullPointerException while working with Eclipse 3.6 Has anybody
In the code below I keep getting a null exception error and have tested
I am getting a null pointer exception before I can even get to my
i keep getting NullPointerException on this line: SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this); i ran some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.