0

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

Deˣ
  • 4,191
  • 15
  • 24
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – SaadAAkash Jul 17 '19 at 17:15

2 Answers2

0

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);
Deˣ
  • 4,191
  • 15
  • 24
java_brat
  • 72
  • 4
0

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