I’ve encountered a problem after I set set my text in Android. Well the scenario goes like this:
I use putExtra from one of my classes then starts intent. on the Second class I declared a String variable which will catch the Extra the code is:
String playername = " ";
String emailaddress = " ";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamepage);
playername = getIntent().getStringExtra("pname");
Log.v(TAG, "Player name set to : " + playername);
emailaddress = getIntent().getStringExtra("eadd");
Log.v(TAG, "Email set to : " + emailaddress);
TextView playerName = (TextView)findViewById(R.id.playerName);
Log.v(TAG, "view fetched");
I used Logs to track the passing of data and it seems to work fine but after adding the part of playerName.setText(playername);
the problem now begins and error logs shows this:
11-22 16:30:50.271: WARN/dalvikvm(4621): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): FATAL EXCEPTION: main
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): java.lang.RuntimeException: Unable to start activity ComponentInfo{arc.android.memorygame/arc.android.memorygame.SetGameScreen}: java.lang.NullPointerException
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.os.Looper.loop(Looper.java:123)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at java.lang.reflect.Method.invoke(Method.java:521)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at dalvik.system.NativeStart.main(Native Method)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): Caused by: java.lang.NullPointerException
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at arc.android.memorygame.SetGameScreen.onCreate(SetGameScreen.java:29)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 16:30:50.397: ERROR/AndroidRuntime(4621): ... 11 more
Is there’s a problem with my code? why does it returns Null Pointer Exception?
Probably
playernameisnullafter this line:getStringExtrawill return null if no string value is found.Try something like this:
Later edit:
If you are absolutely certain that
playernameis notnullat that point, it means thatfindViewByIdreturnednullfor some reason (a typo, an error in yourxmlor whatever).And, by the way, if you have logs and can check for yourself if a variable is
nullor not, why don’t you do it instead of asking? ANullPointerExceptionis not that hard to track down, especially when you only have a few variables.