0

code in RegisterActivity1:

Bundle extras = new Bundle();

...

case R.id.continue_button:

extras.putString("email",eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);

break;

code in RegisterActivity2:


Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here

semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");

I'm trying to pass email and password from activity 1 to activity 2, but nothing I have tried so far seems to be working. I always get a Nullpointer Exception here:

Bundle extras = getIntent().getExtras();

Any tips on how to fix this?

Here is the full method from Activity1, in case it has something to do with the override method...

@Override
public void onClick(View view) {

    String email = eMail_eingabe.getText().toString().trim();
    String password = passwort_1_eingabe.getText().toString().trim();


    if (eMail_eingabe.getText().toString().isEmpty()) {
        //eMail_eingabe.setError("Bitte email eingeben");
        eMail_eingabe.setText("Bitte email eingeben");
        eMail_eingabe.requestFocus();
        return;
    }


    if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        eMail_eingabe.setError("Bitte eine gültige E-Mail eingeben");
        eMail_eingabe.requestFocus();
        return;
    }


    if (password.length() < 6) {
        passwort_1_eingabe.setError("Bitte mindestens 6 Zeichen eingeben");
        passwort_1_eingabe.requestFocus();
        return;
    } else if (passwort_1_eingabe.getText().toString().isEmpty()) {
        passwort_1_eingabe.setError("Bitte passwort eingeben");
        passwort_1_eingabe.requestFocus();
        return;
    } else if (passwort_2_eingabe.getText().toString().isEmpty()) {
        passwort_2_eingabe.setError("Bitte passwort eingeben");
        passwort_2_eingabe.requestFocus();
        return;
    }

    if (!passwort_1_eingabe.getText().toString().equalsIgnoreCase(passwort_2_eingabe.getText().toString())) {
        passwort_2_eingabe.setError("Passwort stimmt nicht überein");
        passwort_2_eingabe.requestFocus();
        return;
    }


    switch (view.getId()) {

        case R.id.continue_button:

            extras.putString("email", eMail_eingabe.getText().toString().trim());
            extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
            extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
            i.putExtras(extras);
            this.startActivity(i);

            break;


    }
}

Exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hoimi, PID: 7812
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hoimi/com.example.hoimi.student.Register2_Student_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3355)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.example.hoimi.student.Register2_Student_Activity.<init>(Register2_Student_Activity.java:22)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1224)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3340)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
aignerbas
  • 45
  • 7

3 Answers3

3
Bundle extras = getIntent().getExtras();

had to be called in the onCreate method, I called it before so it didn't work.

Someone gave the right answer, but deleted his post later (because he got 2 downvotes??)

Anyways, thanks alot.

aignerbas
  • 45
  • 7
1

Problem is not because of getExtras() its because of getIntent(). Check your getIntent() with null and use further.

Your code should be something like

if(getIntent() != null) {
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
}

Good luck.

Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69
0

when you call getIntent(), it returns the intent that you created in previous Activity.

public Intent getIntent ()

Return the intent that started this activity.

Activity documentation

so in second activity, your intent is null

in onCreate initiate your intent

Community
  • 1
  • 1
Ehsan msz
  • 1,774
  • 2
  • 13
  • 26