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

Ioannis Makrigiannis
Ioannis Makrigiannis
508 Points

java.util.NoSuchElementException

Sorry that I have to open another thread about this. I have read them but I still don´t understand what I did wrong. Can anyone help me?

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 mPezCount == 0;
  }

  public void load() {
   mPezCount = MAX_PEZ;
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}

I load this with java-repl and enter: "PezDispenser pd = new PezDispenser("Yoda");"

This is the error message: "java.util.NoSuchElementException"

1 Answer

Harry James
Harry James
14,780 Points

Hey Loannis!

The syntax in Java is case-sensitive, you seem to have missed out the capital for mPezCount in your constructor:

  public PezDispenser(String characterName) {
    mCharacterName = characterName;
    mPezcount = 0; // <-- Check this!
  }

If you correct this, the error should go away and you should then be able to carry on with the challenge.


Let me know if you run into any more issues and I'll be happy to help :)

Ioannis Makrigiannis
Ioannis Makrigiannis
508 Points

Wow you are the boss! Thank you very much for your fast help! Everything works now=)