Null Pointer Exception


#1

Good Afternoon,

I’m working on the exercise in the Model-View-Controller chapter and getting the following null pointer exception:

5-30 19:56:20.996: E/Trace(1467): error opening trace file: No such file or directory (2)
05-30 19:56:21.546: I/System.out(1467): question=2131034120
05-30 19:56:21.546: D/AndroidRuntime(1467): Shutting down VM
05-30 19:56:21.546: W/dalvikvm(1467): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-30 19:56:21.556: E/AndroidRuntime(1467): FATAL EXCEPTION: main
05-30 19:56:21.556: E/AndroidRuntime(1467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.os.Looper.loop(Looper.java:137)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 19:56:21.556: E/AndroidRuntime(1467): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 19:56:21.556: E/AndroidRuntime(1467): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 19:56:21.556: E/AndroidRuntime(1467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 19:56:21.556: E/AndroidRuntime(1467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 19:56:21.556: E/AndroidRuntime(1467): at dalvik.system.NativeStart.main(Native Method)
05-30 19:56:21.556: E/AndroidRuntime(1467): Caused by: java.lang.NullPointerException
05-30 19:56:21.556: E/AndroidRuntime(1467): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:39)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.Activity.performCreate(Activity.java:5104)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-30 19:56:21.556: E/AndroidRuntime(1467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-30 19:56:21.556: E/AndroidRuntime(1467): … 11 more

Its being thrown by the last line of this snippet:

[code]mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getQuestion();

System.out.println(“question=”+question);
mQuestionTextView.setText(question);[/code]

I print out the value of question and its: 2131034120

Not sure why the exception is being thrown? Can you assist?

thanks,


#2

It looks like mQuestionTextView is null. This means that the layout doesn’t include a view with the id question_text_view.

Double check your layout file to ensure that everything is there and has the right id, and make sure that your activity is inflating the correct layout file.


#3

Thank’s for the quick reply. Found the typo in the layout file.


#4

Thank’s for the quick reply. Found the typo in the layout file.