简体   繁体   中英

java.lang.IllegalArgumentException: n must be positive Android Studio

I'm working on a Asteroid dodger game in Android Studio. I create random positions for each of the asteroids using the in build random generator in java. I keep getting java.lang.IllegalArgumentException: n must be positive , however it still runs fine on my phone.

Exception:

java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Random.java:300)
    at ca.alnitakstudios.Asteroids.Asteroids.<init>(Asteroids.java:38)
    at ca.alnitakstudios.Screens.GameScreen.<init>(GameScreen.java:36)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:381)

Code:

public class Asteroids {

private Random random;

private int amountOfAsteroids, fastestSpeed, slowestSpeed,
biggest, smallest;
private int[] x, y, speeds, radii;

public Asteroids() {
    amountOfAsteroids = 5;
    x = new int[amountOfAsteroids];
    y = new int[amountOfAsteroids];

    speeds = new int[amountOfAsteroids];
    radii = new int[amountOfAsteroids];

    fastestSpeed = 7;
    slowestSpeed = 3;

    biggest = 20;
    smallest = 10;

    random = new Random();

    for (int i = 0; i < amountOfAsteroids; i++) {
        //init x & y placement
        x[i] = random.nextInt(GameScreen.width * 2) + GameScreen.width;
        y[i] = random.nextInt(GameScreen.height);

        //Setting speeds
        speeds[i] = random.nextInt(fastestSpeed - slowestSpeed) + slowestSpeed;

        //Setting the radii
        radii[i] = random.nextInt(biggest - smallest) + smallest;
    }
}

public void draw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.GRAY);

    for(int i = 0; i < amountOfAsteroids; i++) {
        x[i] -= speeds[i];
        canvas.drawCircle(x[i], y[i], radii[i], paint);
    }

    checkIfOutOfBounds();
}

public void checkIfOutOfBounds() {
    for(int i = 0; i < amountOfAsteroids; i++) {
        if(x[i] < 0) {
            //x & y placement after its been out of bounds
            x[i] = random.nextInt(GameScreen.width * 2) + GameScreen.width;
            y[i] = random.nextInt(GameScreen.height);

            //Setting new speeds
            speeds[i] = random.nextInt(fastestSpeed - slowestSpeed) + slowestSpeed;
            //Setting new radii
            radii[i] = random.nextInt(biggest - smallest) + smallest;
        }
    }
}

}

n must be positive   at java.util.Random.nextInt

You pass a value that is a negative to the nextInt method make sure that it is not negative when you deduct the fastestSpeed - slowestSpeed and biggest - smallest . Or you can also use Math.abs to make all negative numbers positive

sample:

Math.abs(fastestSpeed - slowestSpeed)

Looking at your stack trace , it appears the problem maybe because you're trying to use static members of GameScreen during the initialization of GameScreen . Are these static members properly initialized to positive values? If they were initialized to 0 , this would be a problem.


Since you're quite certain that it should always be positive, yet somehow you still get zero or negative values, I suggest you create a dummy Random class to debug.

package ca.alnitakstudios.Asteroids;

public class Random {
    private java.util.Random r = new java.util.Random();

    public int nextInt(int i) {
        //Debugging (enable if you need to trace)
        //new Exception().printStackTrace();

        //(or you can also:)
        //StackTraceElement ste = new Exception().getStackTrace()[1]; //the caller
        //print out ste.getClassName(), ste.getMethodName(), ste.getLineNumber() etc.

        System.err.println("nextInt called with value " + i);
        return r.nextInt(i);
    }
}

This should override the Random class used in your Asteroids class, or otherwise you may explicitly import it. No other code changes will be necessary, so you'll know for sure that you're not inadvertently fixing anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM