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

java.util.NoSuchElementException (not a duplicate request AFAIK)

Hello, I'm working on helper Methods and Constructs and am stuck on the step 3 minutes in.

Respectfully, there's a lot of other threads but they don't seem to pertain to my issue or are too complicatedly explained.

Snapshot of workspace: https://w.trhou.se/p8lznlm35r

Code:

public class PezDispenser {
  public static final int MAX_PEZ = 12;

  private String mCharacterName;  
  private

  public PezDispenser(String characterName) { 
    mCharacterName = characterName; 
    mPezCount = 0;
  }

  public boolean isEmpty() {
    return mPezCount ==0;
  }

  public void load {
   mPezCount = MAX_PEZ; 
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}

And error strings:

java> :load PezDispener.java                                                                                                                                                            
Could not load source file from PezDispener.java.                                                                                                                                       
  java.io.FileNotFoundException: PezDispener.java (No such file or directory)                                                                                                           
java> :load PezDispenser.java                                                                                                                                                           
Loaded source file from PezDispenser.java                                                                                                                                               
java> PezDispenser pd = new PezDispenser                                                                                                                                                
java.util.NoSuchElementException        

Furthermore, how should I troubleshoot this on my own outside of Treehouse, when there's no ^ symbol to indicate the error. I'm sure later on I'll have enough exposure to do it on my own but right now this is what Oracle says:

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

I have no clue what that means yet.

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Have you tried to compile that code?

The 5th line of code there has a bare keyword

private

You also need to include the ( and ) when creating a new instance.

PezDispenser pd = new PezDispenser();

Hope that helps!

Hi,

I followed the directions in the video, how did you compile the code? In previous lessons you used the clear && javac NAME.java && jave NAME to run the script. It's my understanding javac was the command to compile.

In the video, we're inside repl, and you never use the javac command.

In the video you use:

:load PezDispenser.java

If you look in my error code, I did that, twice (first time had an error due to MY user error).

I just removed the private field and tried again here is the result:

treehouse:~/workspace$ java-repl                                                                                                                                                 
Welcome to JavaREPL version 303 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)                                                                                               
Type expression to evaluate, :help for more options or press tab to auto-complete.                                                                                               
java> :load PezDispenser.java                                                                                                                                                    
Loaded source file from PezDispenser.java                                                                                                                                        
java> PezDispenser pd = new PezDispenser("Yoda");                                                                                                                                
java.util.NoSuchElementException  

Where is this code supposed to reference:

PezDispenser pd = new PezDispenser();

In the video at mark 3:30 you input:

PezDispenser pd = new PezDispenser("Yoda"); 

I appreciate the response, but it didn't solve the issue as explained.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Yes, to compile your code you can do this:

javac PezDispenser.java

Now when I did this I got

treehouse:~/workspace$ javac PezDispenser.java                         
PezDispenser.java:16: error: '(' expected                              
  public void load {                                                   
                   ^                                                   
1 error      

Do you see what is missing from that error message?

The REPL indeed does a bad job of reporting syntax errors. If you get strange errors from the REPL, remember to check the output from the compiler usingjavac.

Hi Craig,

In the video you don't have to compile, it might be a good note to explain the repl issues, if you look at the comments a lot of people had the similar error I did with no solution. Thank you for clarifying that, it wasn't a case of "google the error code" and get an easy answer. "Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration." was the information provided by Oracle, which is certainly not something we've covered in the course so far.

The advice to test in compile is further confusing, since when I compiled my edited code I got:

Error: Main method not found in class PezDispenser, please define the main method                                                                                                       
   public static void main(String[] args)                                                                                                                                               
or a JavaFX application class must extend javafx.application.Application       

Out of curiosity I loaded java-repl since I compiled and saved my code and I am able to run it inside repl, as of typing this you've help me get past the 3:00 mark to the 3:20 mark successfully:

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.
treehouse:~/workspace$ java-repl                                                                                                                                                        
Welcome to JavaREPL version 303 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)                                                                                                      
Type expression to evaluate, :help for more options or press tab to auto-complete.                                                                                                      
java> :load PezDispenser.java                                                                                                                                                           
Loaded source file from PezDispenser.java                                                                                                                                               
java> PezDispenser pd = new PezDispenser("Yoda");                                                                                                                                       
PezDispenser pd = PezDispenser@366357fd                                                                                                                                                 
java> pd.isEmpty();                                                                                                                                                                     
java.lang.Boolean res1 = true      

I genuinely appreciate your help here, but I'm still confused how I was supposed to solve this without help.

Are you able to provide us with the code for the workspaces so we can analyze and compare our work? It's hard to compare the code in a video to the code in the workspace, and as demonstrated, some errors are just not solvable with my experience. I'm very good with google too, I'm just a sucky/new programmer.

Thanks again Craig, not trying to be argumentative just trying to understand why I can get so layed up. Trying to do 3 lessons and day and if I have to wait 48 hours for the chance of an answer (what the support staff told me today) I feel like that's enough time to lose interest or forget where I left off. Thanks for helping empower me as a learner! (and (joking), I'm pretty polite, not calling anyone a dork so we don't have to filter for that! :)