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

Not getting any output when i have copied all the code from the video.

Hello.

I am currently following the java course. In particular the java object course, the creating the MVP part and the video prompting for guesses. I haven't been using the workspace provided by treehouse rather i have been using netbeans. I have noticed that the console method and class doesn't work for netbeans. So alternatively i have been using the class and methods Scanner. So i have copied the exact code from the video just replacing console for scanner. The problem that i am having is that although the program runs i am not receiving any output and i have copied the exact code from the treehouse video on to netbeans. Here it is :

package hangman; import java.util.Scanner;

public class Prompter { private Game game;

public Prompter(Game game) { this.game = game;
}

public boolean promptForGuess() { Scanner scanner = new Scanner(System.in); String guessAsString = scanner.next("Enter a letter: "); char guess = guessAsString.charAt(0); return game.applyGuess(guess); } }

Could you please advise me what to do with scanner and also how to do it to gain an output. Could you also explain any differences that the treehouse workspace has compared to any regular IDE's such as netbeans.

Thank You

https://teamtreehouse.com/community/cheatsheet

Can you please format your code so it is easier to read.

2 Answers

Rakib, puzzled why you say "I have noticed that the console method and class doesn't work for netbeans."

Try this. First import the class:

import java.io.Console;

This line must be at the very top. Then create a Console object in your promptForGuess() method:

Console console = System.console();

You should now be able to do this:

String guessAsString = console.readLine("Type a character: ");

in the method.

This all works fine in my IDE, so I'd be really surprised if it doesn't work in your netbeans.

Hello

Thank you for your reply. I have added the code that you have sent but it still doesn't run on netbeans. Before running the program there aren't any highlighted errors. However when I run the program the error that appears is

"Exception in thread "main" java.lang.NullPointerException at hangman.Prompter.promptForGuess(Prompter.java:15) at hangman.HangMan.main(HangMan.java:9) Java Result: 1"

This is the code written in both those places

(Prompter.java:15) = String guessAsString = console.readLine("Enter a letter: ");

(HangMan.java:9) = boolean isHit = prompter.promptForGuess();

Still unsure as to why it's not running.