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 trialGonzalo Torres del Fierro
Courses Plus Student 16,751 Pointsjava.util.NoSuchElementException
when i was using the java.repl tool, i was trying to create a new PezDispenser but show me the error "java.util.NoSuchElementExeption"..anybody knows why, it is my foult or something else?
5 Answers
Simon Coates
28,694 PointsIf you google the exception, Oracle's explanation is "Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.". The exception is typically a good clue regarding what you should be looking for. Otherwise, pasting a code snippet would help us debug your issue.
typetypetypetype
Courses Plus Student 1,033 PointsAre you kidding me? How are we supposed to know what that means? We're paying for this site, not for users to copy paste confusing jargon we're not exposed to and tell us to research it.
Simon Coates
28,694 Pointsin response to typetypetypetype: at the time i posted that i don't think i had code to look at. Absent this, hoping the technical explanation might trigger something with Gonzalo Torres del Fierro seemed my best bet. The first step to debugging is to recreate the error. Absent code and the precise actions being taken to produce the fault, it's difficult to assist. FYI, i don't think i was advocating using eclipse to run the problem, but if the code works fine in another environment, it might suggest your classes are written properly.
typetypetypetype
Courses Plus Student 1,033 PointsIn reply to Simon:
To be clear, I was confused if you were kidding or not, not being aggressive :)
I know what Eclipse is because I've watched external videos but here's the thing:
This site pitches itself as a way for someone with no coding experience to jump into coding. I have some experience with Python, but none with Java. So why would eclipse even matter? It hasn't been introduced into Craig's course.
Every lesson I take gives errors that aren't easily solved or explained, and I quite often catch MY OWN errors such as misplaced ; or missing ;, but I still feel like the system isn't addressing core concerns of new programmers.
Simon Coates
28,694 Pointsin response to typetypetypetype: you sounded maybe more frustrated. Sorry about the eclipse business. I misread your exchange with Jessica Williams and was worried i might have accidentally driven people to use eclipse (i mistakenly thought she'd abandoned workspaces to follow in Eclipse). You should be able to get by with workspaces.
As for the difficulty: It's not your imagination. Java's a tricky beast. It's pretty technical (lots of syntax, 1000s of classes, strict). I just looked online at a book called Java: A beginner's guide. It's 700 pages. The tutor is clearly glossing over a lot of complexity - ie. the necessary details of how things work.
typetypetypetype
Courses Plus Student 1,033 PointsIn response to Simon again, thank you, and I talked to Craig who gave some vital information:
If you're getting that error in repl, you compile outside of repl only to troubleshoot it. I didn't understand why people said to compile when he didn't have to stop and compile inside repl. Once I compiled outside of repl, I found my error very easily.
Confused, more than frustrated, the frustration is against paying $50 a month for a site that's no better or worse than the free solutions out there :D
Appreciate your attempts to help either way! No hate to the community!
muhammad sohaib
5,477 PointsHi i, am having same issue when i try to create a new PezDispenser it give me this error(java.util.NoSuchElementException). bellow is the code snippet. Thanks
public class pizDispenser { public static final int MAX_PIZ= 12; private String mCharacterName ; private int mPezCount = 0;
public void load { mPezCount = MAX_PIZ; }
public pizDispenser (String characterName){ mCharacterName = characterName; mPezCount; }; public String getCharacterName() { return mCharacterName; };
};
Simon Coates
28,694 Pointsis this in reference to a challenge or something? I can use a similar set of code to our to create a PezDispenser object (see the main method) in eclipse. In some other environment, you might need to get rid of the system.out.println.
public class PezDispenser {
public static final int MAX_PEZ= 12;
private String mCharacterName ;
private int mPezCount = 0;
public void load() {
mPezCount = MAX_PEZ;
}
public static void main(String[] args)
{
PezDispenser pez = new PezDispenser("Yoda");
System.out.println(pez.getCharacterName());
}
public PezDispenser(String characterName){
mCharacterName = characterName;
//mPezCount; //not sure what this line does.
}
public String getCharacterName() {
return mCharacterName;
}
}
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Pointstreehouse:~/workspace$ java repl
Error: Could not find or load main class repl
treehouse:~/workspace$ java-repl
Welcome to JavaREPL version 303 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)
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= PezDispenser("Yoda");
java.util.NoSuchElementException
java>
as you can see, the las message show me en erro that can use the command to create a new PezDispenser... why?
Simon Coates
28,694 Pointsdon't know, but your code is missing the new keyword prior to the the attempt to create a PezDispenser. Otherwise, could you post your code (consult the markdown cheatsheet) and whether this is in reference to a treehouse challenge (if so, which).
Jason Anello
Courses Plus Student 94,610 PointsYou can post a snapshot of your workspace if you're still having trouble with this.
Jessica Williams
730 PointsI'm having the same issue when following along with the video with Craig. My code in java-repl is:
java> :load PezDispenser.java
Loaded source file from PezDispenser.java
java> PezDispenser pd = new PezDispenser("Yoda");
java.util.NoSuchElementException
java>
I compared my code to Craig's code; however, I'm still getting the same issue. The code for the class we're using follows:
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; } }
Simon Coates
28,694 Pointsthe code in PezDispenser looks okay. I copied it into https://repl.it/languages/java and ran it without issue. Tested population and that every method seemed to work. I just tried it in workspaces. I copied your code across and saved it. Then ran the following:
treehouse:~/workspace$ javac PezDispenser.java
treehouse:~/workspace$ java-repl
Welcome to JavaREPL version 303 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)
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 pf = new PezDispenser("Yoda");
PezDispenser pf = PezDispenser@14cfeedc
java> pf.getCharacterName();
java.lang.String res1 = "Yoda"
java>
Terminating...
treehouse:~/workspace$
John Swan
1,089 PointsMy code is the same, and I am getting the same error
Jessica Williams
730 PointsOkay. I understand what's being done, so I just continued watching the video from that point and just stopped working in the WorkSpace. Thanks Simon!
typetypetypetype
Courses Plus Student 1,033 PointsHi,
Are you saying the solution is to just not use the workspace we're paying for?
David McCarty
5,214 PointsDavid McCarty
5,214 PointsA bit more information/a snippet of the code you are working with would go a long way in helping us figure out what's wrong :)