This site requires JavaScript, please enable it in your browser!
Greenfoot back
patrick26
patrick26 wrote ...

2014/3/9

How to Fix Error Message

patrick26 patrick26

2014/3/9

#
Hi, I keep getting this error message and am having trouble fixing it: java.lang.IllegalArgumentException: n must be positive This is my code: public BalloonWorld() { super(500, 600, 1); for(int d=0; d<5; d++) { addObject(new Balloon(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()/4)); } }
danpost danpost

2014/3/9

#
What line does the error occur on? is there not more code to that class?
patrick26 patrick26

2014/3/9

#
It occurs in this line: addObject(new Balloon(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()/4)); The rest of the code to that class: public class BalloonWorld extends World { public BalloonWorld() { super(500, 600, 1); addObject(new Cactus(), getWidth()/2, getHeight()-50); // add 5 balloons to the world at start-up: for(int d=0; d<5; d++) { addObject(new Balloon(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()/4)); } } public void addBalloons(int howMany) { for(int b=0; b<howMany; b++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(150); addObject(new Balloon(), x , y ); } } }
danpost danpost

2014/3/9

#
I created a world, moved the code above into it and ran it with no problems. What is the entire error message. Copy/paste it into your post.
patrick26 patrick26

2014/3/9

#
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalArgumentException: n must be positive at java.util.Random.nextInt(Random.java:300) at greenfoot.Greenfoot.getRandomNumber(Greenfoot.java:145) at Balloon.act(Balloon.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalArgumentException: n must be positive at java.util.Random.nextInt(Random.java:300) at greenfoot.Greenfoot.getRandomNumber(Greenfoot.java:145) at Balloon.act(Balloon.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/3/9

#
The error is not in your BalloonWorld class. It is in your Balloon class at line 28 (in the 'act' method). This is what is indicated here: at Balloon.act(Balloon.java:28)
You need to login to post a reply.