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

Error java.util.NoSuchElementException

I have been looking for a misspelling or something missing from the code, but can't seem to see anything.

My Code:

public class PezDispenser {
  public static final int MAX_PEZ = 12;
  private String mCharacterName;
  private int mPezCount;

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

  public String getCharacterName() {
    return mCharacterName;
    mPezCount = 0;
  }

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


  public void load() {
    mPezCount = MAX_PEZ;
  }
}

The Console Error:

treehouse:~/workspace$ java-repl
Welcome to JavaREPL version 292 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_51)
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

3 Answers

Brendon Butler
Brendon Butler
4,254 Points

I've only ever seen this Exception on team treehouse, so maybe it's the way they compile things. But it looks like since you didn't initialize your variable "mPezCount" it just can't run properly. Giving you a runtime error.

From what I see, you put your "mPezCount = 0;" in your "getCharacterName()" method. It would also never be run since it was placed after a return statement.

Answer: Basically all you need to do is cut that line from the "getCharacterName()" method, then paste it to your constructor method (the public PezDispenser(String characterName) method).

Thank You!! Worked perfectly.

This same thing happened to me, but my code was already arranged like you told Howard. Hmmm...

This same thing happened to me, but my code was already arranged like you told Howard. Hmmm...

public class PezDispenser { public static final int MAX_PEZ = 12; private String mcharacterName; private int mPezCount;

public PezDispenser(String charName) { mcharacterName = charName; mPezCount = 0;

}

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

}

public void load() { mPezCount = MAX_COUNT; }

public String getName() { return mcharacterName; } } I got the same error.

Benjamin Gooch
Benjamin Gooch
20,367 Points

Thomas,

First, and this is just for naming conventions' sake, I noticed you didn't correctly camel case your "mcharacterName" variable. This won't cause any problems unless you are inconsistant with it, but it looks like you stuck with that naming for all instances. For better readabillity, it should be "mCharacterName". Just a good practice to keep.

Second, I noticed you initailized "MAX_PEX" at the top then tried to recall it as "MAX_COUNT" in the load() method.

It should work after that is corrected.

Also, since I can't see your Example.java file, I'm not sure if you had an issue with calling the methods between the two files or not. For example, you used "getName() in your last method. Make sure in the Example file you used the same method name instead of something like "getCharacterName()" or "getCharName()" or it won't know what you're referring to.