-1

I have written a method to update a user's password in SQLite.

The method is updatePassword()

public class changePassword extends Activity {  
    SQLiteDatabase checkDB;     

    //Load session name 
    String sessionName;
    String sessionPassword;
    String currentPasswordString;
    String newPassword1String;
    String newPassword2String;
    //Load text fields
    EditText currentPassword;
    EditText newPassword1;
    EditText newPassword2;      

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.changepassword);
        Button changePassword = (Button)findViewById(R.id.changePasswordButton);
        //Load session name
        SharedPreferences preferences = getSharedPreferences("temp", getApplicationContext().MODE_PRIVATE);
        sessionName = preferences.getString("sessionName", ""); 
        sessionPassword = preferences.getString("sessionPassword", ""); 
        currentPassword = (EditText)findViewById(R.id.currentPassword);
        newPassword1 = (EditText)findViewById(R.id.newpassword1);

        newPassword2 = (EditText)findViewById(R.id.newpassword2);


    }

    //Mismatched password checking
    public void updatePassword(View v)
    {

        currentPasswordString = currentPassword.getText().toString();
        newPassword1String = newPassword1.getText().toString();
        newPassword2String = newPassword2.getText().toString();
        Toast.makeText(changePassword.this, sessionName.toString(), Toast.LENGTH_LONG).show();
        Toast.makeText(changePassword.this, newPassword2String.toString(), Toast.LENGTH_LONG).show();
        if (currentPasswordString.equals(sessionPassword))
        {
            if (newPassword1String.equals(newPassword2String))
            {

            checkDB.execSQL("UPDATE Students SET Student_Password=" + newPassword2String + "WHERE CNumber=" + sessionName);
            Log.d("Password", "Changed");
            }
        }
    }

}

When this method runs, if I comment out the SQL statement it will display the Log, so that suggests it is running through the if statement without a problem?

I also have toasts to display the variables used in the if's and in the SQL statement however they show the desired values and are not null!

As soon as I comment execSQL back in, this LogCat error shows:

04-08 16:22:55.973: E/AndroidRuntime(4150): FATAL EXCEPTION: main
04-08 16:22:55.973: E/AndroidRuntime(4150): Process: com.example.project, PID: 4150
04-08 16:22:55.973: E/AndroidRuntime(4150): java.lang.IllegalStateException: Could not execute method of the activity
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.view.View$1.onClick(View.java:3823)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.view.View.performClick(View.java:4438)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.view.View$PerformClick.run(View.java:18422)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.os.Handler.handleCallback(Handler.java:733)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.os.Looper.loop(Looper.java:136)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.app.ActivityThread.main(ActivityThread.java:5001)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at java.lang.reflect.Method.invoke(Method.java:515)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at dalvik.system.NativeStart.main(Native Method)
04-08 16:22:55.973: E/AndroidRuntime(4150): Caused by: java.lang.reflect.InvocationTargetException
04-08 16:22:55.973: E/AndroidRuntime(4150):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at java.lang.reflect.Method.invoke(Method.java:515)
04-08 16:22:55.973: E/AndroidRuntime(4150):     at android.view.View$1.onClick(View.java:3818)
04-08 16:22:55.973: E/AndroidRuntime(4150):     ... 11 more
04-08 16:22:55.973: E/AndroidRuntime(4150): Caused by: java.lang.NullPointerException
04-08 16:22:55.973: E/AndroidRuntime(4150):     at com.example.project.changePassword.updatePassword(changePassword.java:69)
04-08 16:22:55.973: E/AndroidRuntime(4150):     ... 14 more
Matt Murphy
  • 265
  • 2
  • 11

1 Answers1

1

If you comment out the

checkDB.execSQL("UPDATE Students SET Student_Password=" + newPassword2String + "WHERE CNumber=" + sessionName);

line and it runs to the log statement, then checkDB is null.

Where do you open/initialize your database?

Darth Android
  • 3,437
  • 18
  • 19