For a project I have to hand in I'm building a a multithreaded Client-Server chat. In one part of this project. I have a server pass an array using the ObjectOutputStream's .writeObject() method, like so:
public void sendOnline(){
try{
//send the array of currently online users to the client
serverOutputStream.writeObject(loggedOn);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
(where loggedOn is a Boolean[] array of 4 elements that indicates which user has logged on with their participant number corresponding to the array.)
The client then receives this array (firstly I'll show the declarations at the top of the client class)
String[] onlineUsers = new String[]{"Arken", "Ben", "Darklark", "Free"};
Boolean[] online = new Boolean[]{false, false, false, false}; // this array will store details of who is online
Boolean[] sendTo = new Boolean[]{false,false,false,false}; //array to hold which user will receive a message
JToggleButton[] buttons = new JToggleButton[]{btnArken, btnBen, btnDarklark, btnFree}; // this will be used with the online array to enable buttons
then the code in the client that is causing me problems comes under a method that is called as the array is received:
public void getUsersOnline(){
try {
online = (Boolean[]) clientInputStream.readObject();
int noOfUsers = 0;
for(int i = 0; i < buttons.length; i++){
if(online[i] == true && i != participantNo){
buttons[i].setEnabled(true); //nullpointerexception
noOfUsers ++;
}
addBroadcast("You have " + (noOfUsers - 1) + " friends online!"); //display number of users online, except for the client itself
for(int j = 0; j < online.length; j++){
if(online[j] && j != participantNo){
addBroadcast(onlineUsers[i] + " is online");
}
}
}
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
System.exit(1);
What I'm trying to do here is iterate through the buttons array and if the element at the same position in the online array is true, to enable the button. (They are both the same length) But for some reason I'm getting a NullPointerException at the .setEnabled line and nothing I've tried has resolved it. Because of the catch statement the code CAN continue but the buttons don't work. Any suggestions?
Thanks!