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

Java Java Objects (Retired) Harnessing the Power of Objects Helper Methods and Conditionals

Nikhil Radhakrishnan
Nikhil Radhakrishnan
1,613 Points

When creating a PezDispenser Object inside the repl it says NoSuchElementException ,Wht to do?

When i write

PezDispenser pd = new PezDispenser("Yoda"); java.util.NoSuchElementException

7 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Try running javac PezDispenser.java and make sure it compiles.

Kenneth Myers
Kenneth Myers
16,009 Points

When I do this I get:

ERROR: ';' expected                                           
    javac PezDispenser.java;                                  
                      ^                                       
ERROR: not a statement                                        
    javac PezDispenser.java;                                  
                       ^                                      

ERROR: ';' expected                                           
    javac PezDispenser.java;                                  
         ^                                                    
ERROR: not a statement                                        
    javac PezDispenser.java;    

EDIT: nevermind found my error, had a typo in one of my mPezCounts and the above error was from trying to do "javac PezDispenser.java" in java-repl and not out of it in the normal console.

Nikhil Radhakrishnan
Nikhil Radhakrishnan
1,613 Points

Craig Dennis U mean to say first i have to compile PezDispenser.java file and then use the :load command , but when we activate the repl we dont compile we just save the file n load. Its confusing !

Craig Dennis
Craig Dennis
Treehouse Teacher

No I mean it seems that if the :load command isn't working, it might be a compile error that it isn't showing up in the REPL. Running javac will let you see the error (if there is one).

Ben Morse
Ben Morse
6,068 Points

When I ran the javac command outside the java-repl, i was able to see my misspelling and undefined variables (more misspelling). After running javac for the third time, I received no errors. I went back into repl command line and typed in PezDispenser pd = new PezDispenser("Yoda"); and it worked. No java.util.NoSuchElementException error.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Sorry, not in the REPL on the command line. Trying to see if there is an error in the PezDispenser.java file.

Chris Vukin
Chris Vukin
17,787 Points

I had this same error, I hadn't removed the = from the public void we created and called return on. I removed the = after return and it worked great!

I was getting this exception thrown because I wasn't loading the .class file. It immediately corrected itself once I typed: ": load PezDispenser.class".

I had this problem for a while, and only when I compiled it did I realize there was an error with isEmpty. The return type is boolean in the method signature, and had return mPezCount = 0 inside the { } which I guess is returning an integer. Everything was fixed once I added another equal sign, so like mPezCount == 0

Clarissa Cabbage
Clarissa Cabbage
2,038 Points

I was having this error as well. I think the problem was that I didn't compile the code before entering the repl. Once I did compile it with javac PezDispenser.java I found that I had an error (I capitalized the "p" in "public"). After correcting my error, everything worked!