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) Creating the MVP Storing Guesses

java.util.NoSuchElementException

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

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

  public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;

    if(isHit){
     mHits += letter;

    }
    else {
     mMisses += letter; 
    }

    return isHit;

  }
}
Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello, I'm a bit confused were you following along in a video and it Gave you the exception. Or was it a certain code challenfe.? Did the error give a line code?

7 Answers

Juan Mendiola
Juan Mendiola
20,751 Points

Hey guys,

I got the same error as well, but solved the issue. I compiled Gama.java to see if any errors would show up. One error did. I think due to editing Craig didn't include a return statement in the applyGuess() method, which got me the error. I corrected this in my code and I can now construct a game object.

I was following the video and then I got that. That is the error it gave me

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Okay, It's returning that it didn't find anything at the index of 0 in your answer I think. In your Hangman.java Class do you have a code that sets the answer? I believe it should look like Game game = new Game("treehouse"); Was that value passed in? Might be the problem.

Yes I added that in my code in Hangman.java

Nevermind, it just decided to work today but I didn't change anything about the code.

Kristina Wahlquist
Kristina Wahlquist
4,519 Points

I am also having the same problem. Below is the code that was included in my Hangman.java file. Wondering if this is a bug with the workspace?

public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
        Game game = new Game("treehouse");
    }

}
Jorge Flores
Jorge Flores
7,864 Points

I was getting the same problem, in my case i tried to compile it and found out a syntax error on my code, after i fixed it i was able to create the object with the java-repl i recommend to check the code and look for errors, i dont think there is a bug on the workspace, good luck :3

Leon Mathis
Leon Mathis
23,404 Points

Just an fyi. I was getting the same error message. Even re-watched the videos to make sure I didn't miss anything. After all attempts, I just restarted my computer and it worked.