-1

Here is all the error message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{johnny.newopen/johnny.newopen.easy}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                            at android.os.Looper.loop(Looper.java:148)
                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
                                                            at johnny.newopen.easy.onCreate(easy.java:126)
                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                            at android.os.Looper.loop(Looper.java:148) 
                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Here is the relevant code:

public class easy extends AppCompatActivity
{
 Button solve;
 private Chronometer chronometer;
 TextView plus1, plus2, minus1, minus2, multy1, multy2, points;
 int p1, p2, m1, m2, mu1, mu2, p, p_ans, m_ans, mu_ans;
 long time;
 private DBHelper mydb;
 boolean check, check1, check2, check3, check4;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_easy);
    mydb = new DBHelper(this);

 if (check1 && check2 && check3) {

        p = p + 60 - (int) time;
    }
    mydb.insertPoints(p);
    points = (TextView)findViewById(R.id.points);
    int x;
    try {
        {x = mydb.getPoints();}
    }
    catch (Exception e) {x=0;}
    points.setText(x);
}
}

I keep getting a null pointer for points. What am I doing wrong?

Manohar
  • 22,116
  • 9
  • 108
  • 144

4 Answers4

1

Looks like your TextView is null at the time of setting text. Looking at your code(incomplete yet), your integer x has not been initialized. Try to run app after setting x=0, also try to check id of textView.
Also you cannot set an integer value to textview, please use String.valueOf()

like this : points.setText(String.valueOf(x))

Sorry for improper formatting of answer; I will fix it later.

Anuj Kumar
  • 1,092
  • 1
  • 12
  • 26
0

Check if the Id of TextView in layout is same as mentioned in activity "(points)" . Also you cannot set an integer value to TextView directly use String.valueOf().

points.setText(String.valueOf(x))
Manohar
  • 22,116
  • 9
  • 108
  • 144
0

You can use x.toString() to set the text value of "points". But you should first check that it exists because I think you're getting the wrong id for the TextView.

Octavian Catusanu
  • 139
  • 1
  • 2
  • 13
0

hey you cannot set int directly to textview

Do simply

points.setText(""+x);

It wil avoid null pointer .

or as Anuj Kumar says you can

**points.setText(String.valueOf(x))**

flashberry
  • 153
  • 11