mercredi 20 mai 2015

An error occurs when signing up a second user

I wrote two java files - on about signup, and the other about storing data. Those are as follows.

  1. signup part

        String entered_email = email.getText().toString();
        String entered_password = password.getText().toString();
        String repassword = rePassword.getText().toString();
        String entered_firstname = firstname.getText().toString();
        String entered_lastname = lastname.getText().toString();
        String entered_dob = dob.toString();
    
        if(entered_email.equals("") || entered_password.equals("") || repassword.equals("") || entered_firstname.equals("") || entered_lastname.equals("") || entered_dob.equals("")) {
            Toast.makeText(getApplicationContext(), "You should not leave the fields empty!", Toast.LENGTH_LONG).show();
            } 
        else if(!entered_password.equals(repassword)) {
            Toast.makeText(getApplicationContext(), "Passwords do not match. Please check it out and type again.", Toast.LENGTH_LONG).show();
            } 
        else if(!entered_email.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) {
            Toast.makeText(getApplicationContext(), "Invalid email. Please enter a valid email address.", Toast.LENGTH_LONG).show();
            }
        else {
            User user = new User(entered_email, entered_password, entered_firstname, entered_lastname, entered_dob);
            user.setEmail(entered_email);
            user.setPassword(entered_password);
            user.setFirstname(entered_firstname);
            user.setLastname(entered_lastname);
            user.setAge(entered_dob);
            dbHelper.signup(user);
            Toast.makeText(this, "You have successfully signed up", Toast.LENGTH_SHORT).show();
            Intent intentMain = new Intent(SignupActivity.this, MainScreenActivity.class);
            startActivity(intentMain);
            finish();
        }
    
    
  2. database helper part

    public void signup(User user) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
    
        String query = "SELECT * FROM " + DB_TABLE;
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();
    
        values.put(COLUMN_ID, count);
        values.put(COLUMN_EMAIL, user.getEmail());
        values.put(COLUMN_FIRSTNAME, user.getFirstname());
        values.put(COLUMN_LASTNAME, user.getLastname());
        values.put(COLUMN_PASSWORD, user.getPassword());
        values.put(COLUMN_AGE, user.getAge());
        db.insert(DB_TABLE, null, values);
        db.close();
    }
    
    

When signing up a first user, it did not have any problems. But when I tried to sign up another user, it showed an error as follows:

05-20 22:20:52.606: E/AndroidRuntime(1117): FATAL EXCEPTION: main
05-20 22:20:52.606: E/AndroidRuntime(1117): Process: com.example.photoalbum, PID: 1117
05-20 22:20:52.606: E/AndroidRuntime(1117): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.photoalbum/com.example.photoalbum.MainScreenActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:135)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:5257)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Native Method)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:372)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-20 22:20:52.606: E/AndroidRuntime(1117): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
05-20 22:20:52.606: E/AndroidRuntime(1117):     at com.example.photoalbum.MainScreenActivity.onCreate(MainScreenActivity.java:27)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.Activity.performCreate(Activity.java:5990)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
05-20 22:20:52.606: E/AndroidRuntime(1117):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
05-20 22:20:52.606: E/AndroidRuntime(1117):     ... 10 more

Can you figure out what the problems are with the codes?

Aucun commentaire:

Enregistrer un commentaire