0
votes

it shows the error on the emulator while playing

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nehagupta.braintrainer/com.nehagupta.braintrainer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.nehagupta.braintrainer.MainActivity.generateQuestion(MainActivity.java:36) at com.nehagupta.braintrainer.MainActivity.onCreate(MainActivity.java:90) at android.app.Activity.performCreate(Activity.java:6857) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) ... 9 more

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nehagupta.braintrainer/com.nehagupta.braintrainer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.nehagupta.braintrainer.MainActivity.generateQuestion(MainActivity.java:36) at com.nehagupta.braintrainer.MainActivity.onCreate(MainActivity.java:90) at android.app.Activity.performCreate(Activity.java:6857) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) ... 9 more

2

2 Answers

0
votes

Check the view ID is same as you defined in the layout.xml file while initializing it in your context.

0
votes

I think that you must initialize your TextView.

If you are in activity, put below code in the onCreate method.

TextView myTextView = findViewById(R.id.your_text_view_id);

Or if you are in Fragment, then in onCreateView insert this line:

TextView myTextView = view.findViewById(R.id.text_view_id);