Reputation: 15734
I have an Activity that I would like to implement an either-or logic to begin with:
I have this line:
Username = getIntent().getExtras().getString("userprofile");
If this is empty, I want it to assign username from somewhere else... such as
if (Username.equals("")) {
Username = Rateit.username;
}
However, it of course crashes on that first line.
Why am I doing this? This Activity will be a User Profile for currently logged in user if the extras is empty. There are other scenarios where you click on other usernames (not you, the logged user), and it goes to SAME Activity with THEIR info.
I either need to find better logic or how to test for null.
LogCat as request on crash:
02-16 14:39:24.088: E/AndroidRuntime(13467): FATAL EXCEPTION: main
02-16 14:39:24.088: E/AndroidRuntime(13467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.---.---/com.---.---.profile.ProfileFragmentActivity}: java.lang.NullPointerException
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.os.Looper.loop(Looper.java:137)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-16 14:39:24.088: E/AndroidRuntime(13467): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 14:39:24.088: E/AndroidRuntime(13467): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 14:39:24.088: E/AndroidRuntime(13467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-16 14:39:24.088: E/AndroidRuntime(13467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-16 14:39:24.088: E/AndroidRuntime(13467): at dalvik.system.NativeStart.main(Native Method)
02-16 14:39:24.088: E/AndroidRuntime(13467): Caused by: java.lang.NullPointerException
02-16 14:39:24.088: E/AndroidRuntime(13467): at com.---.---.profile.ProfileFragmentActivity.onCreate(ProfileFragmentActivity.java:85)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.Activity.performCreate(Activity.java:5008)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-16 14:39:24.088: E/AndroidRuntime(13467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-16 14:39:24.088: E/AndroidRuntime(13467): ... 11 more
Upvotes: 0
Views: 66
Reputation: 22342
If there aren't any extras, getExtras()
returns null. So, it will throw an exception on getString()
. You need to split up that line to check it for null.
userName = "";
Bundle extras = getIntent().getExtras();
if(extras != null)
{
userName = extras.getString("userprofile", "");
}
Also notice the added ""
argument in getString()
, to avoid a null return there. This only works in API >= 12. The one-argument getString()
available in all API levels will return null if the "userprofile"
string extra isn't found, so you'll just need to null-check the returned string.
Upvotes: 2