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

in repl PezDispenser pd = new PezDispenser("Yoda"); I get : java.util.NoSuchElementException ??

I'm trying to follow along with video and I changed all the code and double checked it

I run java-repl and load PezDispenser.java When I type PezDispenser pd = new PezDispenser("yoda"); I get the error message java.util.NoSuchElementException

I've been trying to get this to work for over an hour

I am also getting the same error!

Aaron Kaye
Aaron Kaye
10,948 Points

Can you post your code for the PezDispenser.java?

MINJUN MOON
MINJUN MOON
7,723 Points

I have the same problem.. I don't know what to do...

Aaron Kaye
Aaron Kaye
10,948 Points

MINJUN MOON Before using the repl and :load, make sure to run javac on the file to see if the compiler gives any errors. You most likely have a syntax error of some sort

MINJUN MOON
MINJUN MOON
7,723 Points

Thank you Aaron! I've solved the problem!

CD Lim
CD Lim
4,782 Points

Thanks Aaron I have solve my problem by first running javac to find out my mistake and then run repl.

3 Answers

Aaron Kaye
Aaron Kaye
10,948 Points

Ok, so I think the issues is a syntax error in your PezDispenser file: Change:

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

to:

public boolean isEmpty() { 
    return mPezCount == 0; 
}
Joey Alban
Joey Alban
2,458 Points
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 don't know if I can post this in the answer section (since it's not an answer) but I've been having the same problem and this is what i my code looks like. I looked up the error and it says something about the value not being determined? The explaination is using terms and methods i know nothing about, but I think this is the closest thing i can understand.

"here are possible cause of java.util.NoSuchElementException in Java:

As per Javadoc NoSuchElementException is thrown if you call nextElement() method of Enumeration and there is no more element in Enumeration. below code will throw java.util.NoSuchElementException because Enumeration of hastable is empty."

I've checked and I really don't see why anything wouldn't have a value to it?

Does mCharacterName need a preset value?

Joey Alban
Joey Alban
2,458 Points

You're awesome Aaron! Thank you!