-2

Here is the error that shows up in the logs:

2020-11-09 08:39:44.056 21967-21967/com.example.braintrainer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.braintrainer, PID: 21967
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7448) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.IllegalArgumentException: bound must be positive
        at java.util.Random.nextInt(Random.java:388)
        at com.example.braintrainer.MainActivity.generateQuestion(MainActivity.java:69)
        at com.example.braintrainer.MainActivity.checkAnswer(MainActivity.java:85)
        at com.example.braintrainer.MainActivity.pressOpt1(MainActivity.java:95)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
        at android.view.View.performClick(View.java:7448) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Here is my java code of the function that causes the error: It is basically a brain trainer app that generates math questions and gives you four options to choose from. You choose an option, and then the next question shows up.

The four pressOpt methods are four buttons on the screen which display the options for the answer.

public void generateQuestion () {
        Button answer1 = (Button) findViewById(R.id.optionOne);
        Button answer2 = (Button) findViewById(R.id.optionTwo);
        Button answer3 = (Button) findViewById(R.id.optionThree);
        Button answer4 = (Button) findViewById(R.id.optionFour);
        TextView scores = (TextView) findViewById(R.id.scoreView);

        qAnswered++;

        scores.setText(Integer.toString(score) + "/" + Integer.toString(qAnswered));


        ArrayList<Button> buttons = new ArrayList<>(asList(answer1, answer2, answer3, answer4));
        int a, b;
        Random rand = new Random();
        int upperbound = 10;
        upperbound = rand.nextInt(50);
        while (upperbound < 2)
            upperbound = rand.nextInt(50);
        Log.i("upperbound", Integer.toString(upperbound));
        a = rand.nextInt(upperbound);
        upperbound = rand.nextInt(50);
        b = rand.nextInt(upperbound);
        answer = a + b;
        Log.i("a", Integer.toString(a));
        Log.i("b", Integer.toString(b));
        TextView question = (TextView) findViewById(R.id.questionView);
        question.setText(Integer.toString(a) + " + " + Integer.toString(b));
        int id = rand.nextInt(4);
        buttons.get(id).setText(Integer.toString(answer));
        for ( int i = 0; i<4; i++) {
            if ( i != id)
            {
                upperbound = rand.nextInt(50);
                while (upperbound < 2)
                    upperbound = rand.nextInt(50);

                int Fa = rand.nextInt(upperbound);
                upperbound = rand.nextInt(50);
                int Fb = rand.nextInt(upperbound);
                int falseAnswer = Fa + Fb;
                buttons.get(i).setText(Integer.toString(falseAnswer));
            }
        }

    }

    public void checkAnswer (boolean answerIs) {
        if (answerIs) {
           //correct
            score++;
        }
        else {
            //incorrect
        }
        generateQuestion();
    }

    public void pressOpt1 (View view) {
        Button answer1 = (Button) findViewById(R.id.optionOne);
        if (Integer.toString(answer).equals(answer1.getText()))
        {
            checkAnswer(true);
        }
        else {
            checkAnswer(false);
        }
    }

    public void pressOpt2 (View view) {
        Button answer1 = (Button) findViewById(R.id.optionTwo);
        if (Integer.toString(answer).equals(answer1.getText()))
        {
            checkAnswer(true);
        }
        else {
            checkAnswer(false);
        }
    }

    public void pressOpt3 (View view) {
        Button answer1 = (Button) findViewById(R.id.optionThree);
        if (Integer.toString(answer).equals(answer1.getText()))
        {
            checkAnswer(true);
        }
        else {
            checkAnswer(false);
        }
    }

    public void pressOpt4 (View view) {
        Button answer1 = (Button) findViewById(R.id.optionFour);
        if (Integer.toString(answer).equals(answer1.getText()))
        {
            checkAnswer(true);
        }
        else {
            checkAnswer(false);
        }
    }
  • 1
    Does this answer your question? [IllegalArgumentException: Bound must be positive](https://stackoverflow.com/questions/32101688/illegalargumentexception-bound-must-be-positive) – Aditya Kurkure Nov 09 '20 at 05:08
  • I suggest you aren't running the code you think youre running. – user207421 Nov 09 '20 at 05:26
  • Bound must be positive - I saw that. But I've even put a check to make sure the bound is greater than 2 (to be sure), but still it doesn't work. The code runs for a couple of times, and then crashes randomly. – Divya Soni Nov 09 '20 at 06:42
  • I am not running the code I think I am running? How so? – Divya Soni Nov 09 '20 at 06:43
  • How so if you haven't saved, haven't recompiled, or haven't redeployed, but @Tobi has the answer. – user207421 Nov 09 '20 at 09:38

1 Answers1

1

I assume this happens when your random returns 0 and you use 0 as next upper bound.

In two occasions you make sure upper bound is > 2, but in others you don't.

I suggest that you change all occurances to:

do { upperbound = rand.nextInt(50); }
while (upperbound < 2)

To make this sure everywhere

Tobi
  • 858
  • 7
  • 15