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 Current Progress

adnanalvee2
adnanalvee2
6,862 Points

Null Pointer Exception in Promptor?

I am coding in Eclipse and coding exactly as the teacher. But as soon as i run my java program it says this error in console "Exception in thread "main" java.lang.NullPointerException at Promptor.promptForGuess(Promptor.java:14) at Hangman.main(Hangman.java:10)"

^^ Line 10 as mentioned in the error is the ishit boolean variable in the main method and line 14 is in Promotrs PromptforGuess method, it points to the string inside the method.

I don't know what these errors are.

Here is my code:

hangman.java public class Hangman {

public static void main(String[] args) {


    Game game = new Game("treehouse");
    Promptor promptor = new Promptor(game);     
    boolean isHit = promptor.promptForGuess();
    if (isHit) {
        System.out.println("We got a Hit");
    } else  {
        System.out.println("Whoops that was a miss");
    }

    //promptor.displayProgress();
}

}

Promptor.java import java.io.Console;

public class Promptor {

private Game  mGame;

public Promptor(Game game) {
    mGame = game;
}

public boolean promptForGuess() {
    Console console = System.console();
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);

}

public void displayProgress() {
    System.out.printf("Try to solve: %s\n", mGame.getCurrentProgress());
}   

}

Game.java

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;

}

public String getCurrentProgress() {
    String progress = "";

    for (char letter: mAnswer.toCharArray()) {
        char display = '-';
        if (mHits.indexOf(letter) >= 0)
            display = letter;
        progress += letter;
    }
     return progress;
}

}

Craig Dennis

Well the interesting thing is that your console object is null. I google and found that System.console() will always return null for an IDE.

Source: http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Yeah unfortunately, several IDEs have not embraced the java.io.Console. You can still code there and then run it locally. You could look into System.in and java.util.Scanner, but I think those end up having some problems too.

I use the Console object specifically because of it's ease of use in building console applications.

Edward C. Young
Edward C. Young
10,323 Points

See Class Console - Javadoc. It's not that the IDE's wont embrace the console, it's that the class is declared as public static final, and therefore cannot be wrapped. Quoting:

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by background job scheduler, then it will typically not have a console.


If I'm understanding correctly, all IDE's launch the Virtual Machine, as either a background process, or using javaw so that the output can be sent to the docked tabs in the IDE. See also: ILLEGAL ARGUMENT EXCEPTION - Java: System.console(), IDEs and testing to see a custom wrapper, and System.console() gives NullPointerException in NetBeans for help on using Scanner

Edward C. Young
Edward C. Young
10,323 Points

I ran into this, while using IntelliJ 2016.2. Sad that even a year later there isn't a way to fix this, but it is possible to still use an IDE, and follow along with the video. You must do the following:

  1. Create a new Console Project from your IDE's New Project Menu. Note that you won't be able to run the Project from within the IDE
  2. Create a Package Bundle, if the New Project Wizard requires it. Most Do, i.e. com.example.hangman
  3. Ensure your Path environment variable for your SYSTEM, NOT YOUR USER, contains the path to javac and java. On Windows this is usually C:\Program Files\Java\<version>\bin. Note that on a 64 bit system, you can use a 32 bit version. If you know what this means, you'll know about (x86). Image of Environment Variables - Path
  4. In your IDE, write the files as shown in the video.
  5. Use the Build menu, to Compile Only (Ctrl + Shift + F9). If the project contains multiple classes/files, you can use Rebuild Project:

Image of Build Options

  1. If no errors occur, your project tree will now contain an out folder, usually colored differently, as to signify that the objects in the directory are generated, not written. Right Click the out folder and use the File Path, or Open in Explorer option.
  2. Go to the top level directory of the out folder, for the project. In my case that is out\production\Hangman in a Command Prompt .
  3. Execute the following command: java com.example.hangman.Hangman. Notice the command uses the Fully Qualified Package Name
Gary Mclean
Gary Mclean
5,756 Points

Thanks for this! worked perfectly.