所以我有一个聊天室,它曾经工作,但我稍微改变了代码,尝试了一些东西,但它们没有用,所以我试图恢复原来的代码,但我不确定我做错了什么因为现在它抛出一个NullPointerException.我的代码有一个PrintWriters的ArrayList和一个方法showAll()就像它说的那样; 向聊天室中的所有人发送消息.所以基本上我想知道的是我怎么得到例外?
//This is the main code, used to add printwriters to the arraylist and to connect to clients
//The Communicate thread is used to get input from users and use the showAll() method with that input
public void listen() {
listWriters = new ArrayList<PrintWriter>();
try {
scanner = new Scanner(System.in);
portnum = getPortNumber(scanner);
System.out.println("Listening on " + portnum);
serverSocket = new ServerSocket(portnum);
while(true) {
clientcommunicate = serverSocket.accept();
System.out.println("Connection accepted: " + clientcommunicate.toString());
PrintWriter client = new PrintWriter(clientcommunicate.getOutputStream(), true);
listWriters.add(client); …
Run Code Online (Sandbox Code Playgroud)