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 trialPaul Joiner
1,682 PointsWhen 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?
8 Answers
Steve Hunter
57,712 PointsSorry, I missed this. Type return
before mBarsCount == 0
Steve.
Paul Joiner
1,682 PointsThanks! It compiled, I will now see if I can follow the video in the REPL
Steve Hunter
57,712 PointsHi 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.
typetypetypetype
Courses Plus Student 1,033 PointsHi 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.
Steve Hunter
57,712 PointsGlad you got it sorted!
Paul Joiner
1,682 PointsHi 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;
}
}
Steve Hunter
57,712 PointsHi 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
9,721 PointsYes, 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
1,682 PointsThis 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
1,682 PointsThanks 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
9,721 PointsYea, 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
Steve Hunter
57,712 PointsHi 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.
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHi Paul,
have compiled the PezDispenser.java ?