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

Error NoSuchElementException in java objects hangman

I am getting the Error: java.util.NoSuchElementException when I type in : Game game = new Game('treehouse');

Here is the link to my workspace: https://w.trhou.se/ddx954dtn0

If someone could help me that would be great!

Make sure that each class being used is saved and compiled. If it is not compiled then the jvm cannot find it.

4 Answers

You need to remove the extra semi-colon after your constructor's parameters:

public class Game {
  private String mAnswer;
  private String mHits;
  private String mMisses;

  public Game(String answer); /* <--- Remove this semi-colon*/ {
   mAnswer = answer; 
   mHits = "";
   mMisses = "";
  }
}

Hi Sophie - You have an extra semicolon in the Game constructor right after ) and it shouldn't be there. The constructor should look like this:

public Game(String answer) {
    mAnswer = answer; 
    mHits = "";
    mMisses = "";
}

Ok so I did that but then when I did Game game = new Game('treehouse'); this error came up this time:

ERROR: cannot find symbol
symbol: class Game
location: interface Evaluation
Game method$jqtnfmskubcepzgr154i();
^

Can you include link to your workspace?

You were able to do Game game = new Game('treehouse'); and it worked fine? if you did can you tell me exactly what you did so I can make sure I'm doing it right?

You were able to do Game game = new Game('treehouse'); and it worked fine? if you did can you tell me exactly what you did so I can make sure I'm doing it right?

I didn't make any changes to your code. Just compiled it and didn't get any error. Why don't you delete the two .class files and recompile Hangman.java and see if you still get the error.

Im still getting the error cannot find symbol. Is it right when I compile Hangman.java then go into the repl and then load Hangman.java then do Game game = new Game('treehouse'); because thats what I'm doing and getting an error from

Did you compile your Game class? (javac Game.java) it won't work without the class file.

I just forked your workspace and compiled Hangman.java and didn't get any errors.

I see - you're trying to do this in repl. The two java files you have are not available in repl unless you load them. So once you're in repl you need to type:

:load Game.java
:load Hangman.java

Then you can create an instance of Game. By the way, after you compile Hangman.java you can just run it in the console:

java Hangman

Of course, you wouldn't see any output by doing that since you're not printing anything in the main method.

I dont know I'm still getting the cannot find symbol error when I do it