1

I'm making a multi-player game where each player has their own input/output console on the screen. I'm having a bit of trouble trying to do this. I don't want every player to see other player's in/outputs.

To use an analogy, I want to do something like playerOneConsole.out.println("Player One String");, instead of System.out.println("Player One String"); where everyone can see player one's stuff.

After reading some documentation, I've tried this, but it does not work as intended as it throws a NullPointerException:

public class Player {

    String myName;
    Console myConsole;

    public Player(String name) {
        myName = name;
        myConsole = System.console();
    }


    public void takeTurn(String playerOptions){
        myConsole.writer().print(playerOptions); //This is not right.
    }
}

I want playerOptions to print exclusively to that player's console, not the System console.

By the way, I'm using NetBeans IDE 8.0.2 if that makes a difference.

3
  • So presumably myConsole is null, because you're running in an environment where System.console() returns null, e.g. in Eclipse? Commented Jun 25, 2015 at 15:27
  • Since there are multiple consoles, they must be implemented somehow, and there must be a way of addressing them separately. On a Linux/Unix system I guess they would ultimately be addressed with separate file handles whether or not implemented in application software, OS software or OS hardware. Which is it? If in application software is it 3rd party of something of your own design? If its 3rd party, take a look in the API for a solution. Commented Jun 25, 2015 at 15:34
  • Maybe you could use pseudo terminals (pty) implemented in Java using Pty4J which provides read/write access but requires running some kind of shell in each pty. It supports Linux, OSX and Windows platforms and is available at github.com/traff/pty4j. Commented Jun 25, 2015 at 16:40

1 Answer 1

6

When using java.io.Console, you must execute the Java app from a console e.g. Windows CMD or Linux Terminal. Most IDEs won't execute Java through a console, so System#console returns null.

By the way, this code:

myConsole.writer().print(playerOptions); //This is not right.

It's right indeed :)

Sign up to request clarification or add additional context in comments.

2 Comments

I've managed to Build my application and run it in Windows command prompt. It runs but simply ignores the line calling myConsole.writer().print(playerOptions);. I should be expecting a new cmd window to open, right? If I run command prompt as administrator, I get this error: Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' has value '1.8', but '1.7' is required. Error: could not find java.dll Error: Could not find Java SE Runtime Environment.
Are you sure the line is executed and it's not empty?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.