Android - Crash on generating random number


Ask by : Aearnus November 04, 2012 01:37

So, I have looked at other places for how to generate a random number, and put it into an Android app. I would expect this to work, but sadly, it crashes with this LogCat:

11-03 17:06:48.930: W/dalvikvm(8327): threadid=1: thread exiting with uncaught exception (group=0x41ff0300)
11-03 17:06:48.938: E/AndroidRuntime(8327): FATAL EXCEPTION: main
11-03 17:06:48.938: E/AndroidRuntime(8327): java.lang.IllegalArgumentException
11-03 17:06:48.938: E/AndroidRuntime(8327):     at java.util.Random.nextInt(Random.java:187)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at com.spng453.randomnum.RandomNumber$1.onClick(RandomNumber.java:24)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at android.view.View.performClick(View.java:4084)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at android.view.View$PerformClick.run(View.java:16966)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at android.os.Handler.handleCallback(Handler.java:615)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at android.os.Looper.loop(Looper.java:137)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at java.lang.reflect.Method.invoke(Method.java:511)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 17:06:48.938: E/AndroidRuntime(8327):     at dalvik.system.NativeStart.main(Native Method)
11-03 17:06:51.376: I/Process(8327): Sending signal. PID: 8327 SIG: 9

I have tried everything I can think of to get this to work, but nothing does. Here's the code, and thanks in advanced.

private OnClickListener Gen = new OnClickListener() {
    public void onClick(View v) {
        Random rand;
        rand = new Random();
        EditText maxin = (EditText)findViewById(R.id.max);
        EditText minin = (EditText)findViewById(R.id.min);
        TextView out = (TextView)findViewById(R.id.out);
        int max = Integer.parseInt(maxin.getText().toString().trim());
        int min = Integer.parseInt(minin.getText().toString().trim());
        out.setText(Integer.toString(rand.nextInt(max-min) + min));
    }
};
View original question

Answer by : Jon SkeetNovember 04, 2012 00:13

Well, that will certainly happen if your max text box has a number smaller than or equal to your min text box. You should validate that before calling Random.nextInt(int). As the docs state:

Throws:
IllegalArgumentException - if n is not positive

Of course you should also handle the situation where your text boxes don't have a valid number in at all.

Finally, is there any reason why you're separating the declaration and assignment of rand?

Random rand;
rand = new Random();

Why not just:

Random rand = new Random();

?

View original answer