-2

Using Android Studio and Java, I'm attempting to build an app witch output different information based on input from previous other buttons clicks. The problem that I'm having is with the if statement inside Switch/Case which crashes the app every time when conditions are not met. In this case button 5 crashes the entire app when the variable bUttn does not equal to "3". When conditions are met it works just fine and it does what it is supposed to do. What am I doing wrong?

Here is a sample of my code:

Package com.example.testjava4;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.Arrays;


public class MainActivity extends Activity implements View.OnClickListener {

    ListView list;
    String[] kakNmbrs = {"1+2+5+8+9", "1+2+6+7+9", "1+3+4+8+9", "1+3+5+7+9", "1+3+6+7+8", "1+4+5+6+9", "1+4+5+7+8",
            "2+3+4+7+9", "2+3+5+6+9", "2+3+5+7+8", "2+4+5+6+8", "3+4+5+6+7"};
    String bUttn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn3 = (Button) findViewById(R.id.button3);
        Button btn4 = (Button) findViewById(R.id.button4);
        Button btn5 = (Button) findViewById(R.id.button5);
        btn3.setOnClickListener(this); // calling onClick() method
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override

    public void onClick(View v) {
        Button bTTon4 = (Button) findViewById(R.id.button4);
        list = findViewById(R.id.myList);
        Arrays.sort(kakNmbrs);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.mytextsize, android.R.id.text1, kakNmbrs);
        switch (v.getId()) {

            case R.id.button3:
                bUttn = "3";
                Log.i(bUttn, "I just clicked on");
                break;

            case  R.id.button4:
                break;

            case  R.id.button5:
               if (bUttn.equals("3")) {
                    list.setAdapter(adapter);
                   bTTon4.setVisibility(View.INVISIBLE);
                break;
                }


        }
    }
}

Here is the logcat log if that would be of any help:

11/11 14:37:59: Launching 'app' on Andi Emu API 22. Install successfully finished in 8 s 374 ms. $ adb shell am start -n "com.example.testjava4/com.example.testjava4.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Connected to process 6809 on device 'emulator-5554'. Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page. D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true D/: HostConnection::get() New Host Connection established 0xb40f1ce0, tid 6809 D/Atlas: Validating map... D/: HostConnection::get() New Host Connection established 0xb43fb330, tid 6825 I/OpenGLRenderer: Initialized EGL, version 1.4 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... D/EGL_emulation: eglCreateContext: 0xb42a4b80: maj 2 min 0 rcv 2 D/EGL_emulation: eglMakeCurrent: 0xb42a4b80: ver 2 0 (tinfo 0xb4272ce0) D/OpenGLRenderer: Enabling debug mode 0 D/EGL_emulation: eglMakeCurrent: 0xb42a4b80: ver 2 0 (tinfo 0xb4272ce0) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testjava4, PID: 6809
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.testjava4.MainActivity.onClick(MainActivity.java:55)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • `java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference` is pretty exaustive: ie `bUttn` is `null` – fantaghirocco Nov 11 '20 at 20:14

2 Answers2

1

The reason it is crashing is because the String variable bUttn only gets initialized in case R.id.button3. This is important because without initialization, bUttn will be null.

So if case R.id.button5 is true, then bUttn will never get initialized since initialization happens in a different case, and calling .equals() on a null String will cause a NullPointerException.

krabinowitz
  • 357
  • 3
  • 11
-1

You just declared the bUttn variable.You didn't initialized the bUttn.That is why it showing NullPointer Exception. Because in if case you are comparing bUttn value with 3.But at that time bUttn has no value.

You should change String bUttn; to String bUttn="" or String bUttn="0";

If bUttn is going to be Int value, you can change datatype of bUttn into Int.

Sathyan
  • 138
  • 1
  • 10