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

Dan B
Dan B
6,155 Points

Added the isEmpty() function which returns mPezCount == 0; now getting this error: "java.util.NoSuchElementException"

So I added the following to my PezDispenser.java

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

Now when I run the following command in the repl:

PezDispenser pd = new PezDispenser("Yoda");

I get this error:

java.util.NoSuchElementException

What have I done wrong? My PezDispenser.java looks the same as the teacher's...

Dan B
Dan B
6,155 Points

I'll answer this myself. I actually had a tiny 'fullstop' in my PezDispenser.java in one of the lines that was throwing the exception. After scanning the file carefully and removing it, and loading it again, it compiled without any issues.

2 Answers

What's wrong with my one

public class PezDispenser {

public String mCharacterName;

public PezDispenser (String characterName) { mCharacterName = characterName; mCountPez = 0;

}

public boolean isEmpty() {

return mCountPez == 0; } pulbic void load() { mPezCount = MAX_PEZ; }

public static final int MAX_PEZ = 12; public int mPezCount;

public String getCharacterName() { return mCharacterName; }

}