I am trying to make a simple TCP chat program, and when the user sends the message "/list", I want to reply from the server with a list of all online users.
else if (cmd.text.equals("/list")){
//Sends the client a list of all registered users
UserList userList = new UserList();
System.out.println(online_users);
if (online_users.values()!=null){
userList.users.addAll(online_users.values());
}
c.sendTCP(userList);
}
However, even though I have tested and know that online_users does contain a value to add to userList.users, the command userList.users.addAll() keeps throwing an error. This is the error:
Exception in thread "Server" java.lang.NullPointerException
at com.andrewlalisofficial.ChatServer.checkCommand(ChatServer.java:128)
at com.andrewlalisofficial.ChatServer$1.received(ChatServer.java:68)
at com.esotericsoftware.kryonet.Server$1.received(Server.java:61)
at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246)
at com.esotericsoftware.kryonet.Server.update(Server.java:208)
at com.esotericsoftware.kryonet.Server.run(Server.java:356)
at java.lang.Thread.run(Thread.java:745)
where line 128 is userList.users.addAll(online_users.values()); I want to be able to simply add the collection of values from the Map online_users to a new String list and send it off. I don't understand why there is an error.