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?

Here is my code in PezDispenser.java :

public class PezDispenser {
  public static final int MAX_PEZ = 12;
  private String mCharacterName;
  private int mPezCount;
  public  PezDispenser(String characterName) {
   mCharacterName = characterName; 
   mPezCount = 0;
  }

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

  }

  public void load() {
    mPezCount = MAX_PEZ; 
  }

  public String getCharacterName() {
   return mCharacterName; 
  }
}

The problem is that whenever I type PezDispenser pd = new PezDispenser("Yoda"); in the java-repl, it says, java.util.NoSuchElementException.

Please help. Thanks!

1 Answer

James Fowler
James Fowler
2,362 Points

For anyone else looking for an answer on this message. The previous answer said there was a missing class. I went back to the previous videos and followed along. I finally decided to press on and found that it was a typo in my code that was causing the message. I was tipped off by googling the error message. It led me to a stack overflow article that mentioned a semicolon in the class. Sure enough, I had put a semicolon at the end of the isEmtpy method.

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

I typed isEmpty();

After I removed the semicolon and saved I was able to go back and finish the exercises in this video.