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

Paul Joiner
Paul Joiner
1,682 Points

When I enter " PezDispenser pd = new PezDispenser("Yoda"); " into repl I get a message:java.util.NoSuchElementException

I can't follow along with the Java Objects video. When I enter java-repl and load the PezDispenser program, I can no longer follow along. What's happening here?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Paul,

have compiled the PezDispenser.java ?

8 Answers

Sorry, I missed this. Type return before mBarsCount == 0

Steve.

Paul Joiner
Paul Joiner
1,682 Points

Thanks! It compiled, I will now see if I can follow the video in the REPL

Hi Paul,

As Gregorij suggested, you need to make sure that you have saved and compiled the code. Also, have you definitely spelled the class PezDispenser? Any slight difference in spelling would cause the same issue.

If it is neither of those things; post a screen shot, or copy/paste your code and the repl text into here and we can sort it out for you.

Steve.

Hi Steve,

I have the same issue right now. Can you explain why Craig didn't compile the code in the video, but just saved and jumped into repl?

Error message I get:

java> PezDispenser pd = new PezDispenser("Yoda");                                                                                                                                       
java.util.NoSuchElementException 

My code:

public class PezDispenser {
  public static final int MAX_PEZ = 12;

  private String mCharacterName;  
  private

  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;
  }
}

UPDATE - I noticed I had a stray ; in this code block;

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

It was after "isEmpty();".

Removing that took care of the error message that's being posted about.

Glad you got it sorted! :+1:

Paul Joiner
Paul Joiner
1,682 Points

Hi everyone, Thanks for your help so far. I've tried compiling PezDispenser.java and I'm running into issues there as well. My code is kind of a mess at this point because I've been backtracking and making various changes to the code in order to try to get it to compile. Any suggestions to what might be wrong here? This is what I have so far:

public class PezDispenser {
  public static final int MAX_PEZ = 12;
  private String mCharacterName;
  private int mPezCount; // add this line here!! 


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

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

  public void load() {
   mPezCount = MAX_PEZ; 
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}

Hi Paul,

You don't have a variable called mPezCount. Try adding this to the other member variables at the top of the class:

private int mPezCount;

I'll put a comment in your code where to put that.

Steve.

Valerie O. Okpoko
Valerie O. Okpoko
9,721 Points

Yes, most definitely check the spelling and make sure everything is in the right case. Java is case sensitive so even if the spelling is the same, it will tell you the element does not exist, even if just one letter is in a wrong case. Also check that you actually created a constructor that takes in a String variable as its parameter. Lastly, if you are creating the PezDispenser object from another class that is not in the same directory as the main PezDispenser class, make sure you import it.

Paul Joiner
Paul Joiner
1,682 Points

This is my error message when I try to compile: treehouse:~/workspace$ javac PezDispenser.java
PezDispenser.java:12: error: not a statement
mPezCount == 0;
^
1 error

Paul Joiner
Paul Joiner
1,682 Points

Thanks Steve, I added the mPezCount variable like you suggested. When I saved and went to compile I got the next error message: PezDispenser.java:13: error: not a statement
mPezCount == 0;
^
1 error

Did I go wrong with the double equals sign? Once again, here's my code. Thanks again!

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

  public void load() {
   mPezCount = MAX_PEZ; 
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}
Valerie O. Okpoko
Valerie O. Okpoko
9,721 Points

Yea, you are not supposed to use double equals sign when you are not comparing the two in like an if statement. equals (=) means assign what is on the right to the left. double equals (==) means compare both

Hi Valerie,

Paul was correctly using the double equals as he was wanting to compare mBarsCount to zero before returning the result of that comparison from the isEmpty method.

The issue was the lack of a return keyword, once we had the member variable created within the class.

Steve.