0
votes

I want the text view to increase by 2.5 every button press, but the app keeps crashing. I tried using the number picker, but I didn't like the way it was oriented vertically. I decided to make my own and later I add the long press capabilities, but now I am experimenting with the buttons.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity implements OnClickListener {

private Button Calculate;
private Button plus;
private Button menus;
private OnClickListener buttonclick;
private TextView textView;
int startweight;
double weight;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    menus = (Button) findViewById(R.id.button3);
    plus = (Button) findViewById(R.id.button2);
    Calculate = (Button) findViewById(R.id.button);

    menus.setOnClickListener(this);
    plus.setOnClickListener(this);
    Calculate.setOnClickListener(this);

    int startweight = 100;

    textView.setText("" + startweight );


}

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button2:
        if (weight != 100) {
            weight = weight +2.5;
        }
        else{
            weight = startweight - 2.5;
        }
        textView.setText("" + weight);
        break;
    case R.id.button3:
        if (weight != 100) {
            weight = weight - 2.5;
        }
        else{
            weight = startweight -2.5;
        }
        textView.setText("" + weight);
        break;
    case R.id.button:

        break;
}

}
}

This is the stack from the logcat:

FATAL EXCEPTION: main Process: com.example.luke.maxbenchcalculator, PID: 2281 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luke.maxbenchcalculator/com.example.luke.maxbenchcalculator.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: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(java.lang.CharSequence)' on a null object reference at com.example.luke.maxbenchcalculator.MainActivity.onCreate(MainActivity.java:39) 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)

1
Any time there is a crash, you should provide the stack trace of the exception as seen in logcat.Doug Stevenson
My bad Doug Stevenson, to be honest I just started using Java 3 days ago and am only a teenager and don't really have any coding experience. I've just been learning as I go.LUKER
It's cool @LukeMueller, now you know for next time. :-)Doug Stevenson
Hey Doug, I put the stack from logcat in now. It would be greatly appreciated if you could take a look. Thanks. @DougStevensonLUKER
Yes, thank you. It's still exactly as I say in my answer - you didn't assign a value to textView, which causes the app to crash because you can't call a method on a null object. If you could accept the answer as correct, I would appreciate it.Doug Stevenson

1 Answers

2
votes

You never assigned a value to the textView member, so it stays null. Then you try to call a method on it during onCreate, which causes a crash. Assign it a value and make sure it's not null before calling a method on it.