-1

I am trying to create an app where a user can login and logout.
When they login, they are presented with an activity that displays their email and name (from a database).

I recently added a navbar back option. When the user goes to the navbar and selects a page (about, settings, etc.), they will go to that Activity. If the back Button is pressed to go to the logged in the Activity (LoginSuccess.java), the app crashes displaying "Unfortunately Frienday's has stopped running."

I have tried to debug this, and it's leading me to a NullPointerException in LoginSuccess.java. I tried removing the items in the LoginSuccess.java file that make the name and email of the user appear. If these are removed, the back button will work fine.

LoginSuccess.java file

TextView name, email;

protected void onCreate(Bundle savedInstanceState) {
    ...
    name = (TextView) findViewById(R.id.name);
    email = (TextView) findViewById(R.id.name);
    Bundle bundle = getIntent().getExtras();
    name.setText(bundle.getString("name"));
    email.setText("Email: " + bundle.getString("email"));
}

login.php file (for database)

<?php
    require "init.php";

    $user_name = $_POST["user_name"];
    $password = $_POST["password"];

    $sql = "SELECT name,email FROM user_info WHERE user_name LIKE   '".$user_name."' AND password LIKE '".$password."';";

    $result = mysqli_query($con, $sql);
    $response = array();

    # Check if user already exists.
    if (mysqli_num_rows($result) > 0)
    {
        $row = mysqli_fetch_row($result);
        $name = $row[0];
        $email = $row[1];
        $code = "login_success";
        array_push($response, array ("code" => $code, "name" => $name, "email" => $email));
        echo json_encode($response);
    }
    else
    {
        $code = "login_failed";
        $message = "User not found... Please try again!";
        array_push($response, array("code" => $code, "message" => $message));
        echo json_encode($response);
    }

    mysqli_close($con);

?>

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.goril.friendays, PID: 32166
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goril.friendays/com.goril.friendays.LoginSuccess}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7237)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               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
                  at com.goril.friendays.LoginSuccess.onCreate(LoginSuccess.java:25)
                  at android.app.Activity.performCreate(Activity.java:6876)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:158) 
                  at android.app.ActivityThread.main(ActivityThread.java:7237) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

I have been struggling with this for a few days. If anyone could help me I would gladly appreciate it :). I implemented the back option from the Android website tutorial.

Tutorial: https://developer.android.com/training/implementing-navigation/ancestral.html

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
D. Ross
  • 3
  • 1

2 Answers2

1

When you press back, getIntent() doesn't exist from the previous Activity that started that activity, therefore it'll be null.

You can catch that like so

Bundle extras = getIntent().getExtras();
if (extras != null) {
    name.setText(bundle.getString("name"));
    email.setText("Email: " + bundle.getString("email"));
}
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
0

I think you have already the explanation there, try to read it :

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

The bundle is null and you are trying to get a string on a null object. You need to check for null.

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
Romulano
  • 236
  • 2
  • 7