Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion Guess How Many Are In The Jar Game in Android

Tyler Combs
Tyler Combs
15,525 Points

I can't get the random element to work.

Hey, everybody!

I can't seem to get the jar to be filled with a random number. No matter which way I set it up, I get an exception that says "bound must be positive". Any advice would be greatly appreciated! I've been on this project for too long :)

Here's a snapshot of my workspace: https://w.trhou.se/fozi8zuct6

Thank you!

Edit: Here's the exception:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive                      
        at java.util.Random.nextInt(Random.java:388)                                                       
        at Prompter.<init>(Prompter.java:8)                                                                
        at Game.<init>(Game.java:2)                                                                        
        at Jar.main(Jar.java:5)

2 Answers

Brendon Butler
Brendon Butler
4,254 Points

Looks like you need to initialize your mMaxItems variable.

When you initialize your newAnswer variable, it makes a call to the mMaxItems variable which is never set.

Tyler Combs
Tyler Combs
15,525 Points

Thanks! I thought since I wanted a variable assigned to mMaxItems by the "admin" that I couldn't pre-assign a variable. I guess it just over-writes it, right?

Brendon Butler
Brendon Butler
4,254 Points

Yeah, the only exception is if you were to make the variable final..

private static final int mMaxItems = 1;

That would make the mMaxItems variable unchangeable.

Tyler Combs
Tyler Combs
15,525 Points

Thanks! Makes sense.