Reputation: 11
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.
Upvotes: 1
Views: 211
Reputation: 85809
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 :)
Upvotes: 6