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

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

java.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?

A 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 :)

5 Answers

Simon Coates
Simon Coates
28,694 Points

If 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.

Are 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
Simon Coates
28,694 Points

in 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.

In 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
Simon Coates
28,694 Points

in 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.

In 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
muhammad sohaib
5,477 Points

Hi 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
Simon Coates
28,694 Points

is 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
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

treehouse:~/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
Simon Coates
28,694 Points

don'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).

You can post a snapshot of your workspace if you're still having trouble with this.

Jessica Williams
Jessica Williams
730 Points

I'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
Simon Coates
28,694 Points

the 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
John Swan
1,089 Points

My code is the same, and I am getting the same error

Jessica Williams
Jessica Williams
730 Points

Okay. 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!

Hi,

Are you saying the solution is to just not use the workspace we're paying for?