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
Sophie Tarpey
1,066 PointsError 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!
4 Answers
Derek Markman
16,291 PointsYou 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 = "";
}
}
Kourosh Raeen
23,733 PointsHi 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 = "";
}
Sophie Tarpey
1,066 PointsOk 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();
^
Kourosh Raeen
23,733 PointsCan you include link to your workspace?
Sophie Tarpey
1,066 PointsSophie Tarpey
1,066 PointsYou 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?
Sophie Tarpey
1,066 PointsYou 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?
Kourosh Raeen
23,733 PointsI 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.
Sophie Tarpey
1,066 PointsIm 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
Jeremy Hill
29,567 PointsDid you compile your Game class? (javac Game.java) it won't work without the class file.
Kourosh Raeen
23,733 PointsI just forked your workspace and compiled Hangman.java and didn't get any errors.
Kourosh Raeen
23,733 PointsI 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.
Sophie Tarpey
1,066 PointsI dont know I'm still getting the cannot find symbol error when I do it
Jeremy Hill
29,567 PointsJeremy Hill
29,567 PointsMake sure that each class being used is saved and compiled. If it is not compiled then the jvm cannot find it.