0

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!

Rymo_18
  • 1
  • 3
  • you havent initialized the instances of `JToggleButton` in `buttons` – Reimeus Dec 07 '15 at 17:30
  • Duplicate of http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – andrucz Dec 07 '15 at 17:36
  • What are `btnArken, btnBen, btnDarklark, btnFree` ? – Yassin Hajaj Dec 07 '15 at 17:37
  • Maybe you didn't initialize the 4 buttons in the buttons array?? – Leet-Falcon Dec 07 '15 at 18:38
  • btn-Name- is just the button for the relevant user of the chat service. JToggleButtons have been initalised. They're initialised in the GUI constructor thats called after a login – Rymo_18 Dec 07 '15 at 18:48
  • Solved the problem. It was that I was adding the buttons into the array but had a mind blank and forgot that initialising the buttons later on doesn't automatically update the array. Whoops! Thanks for the help! – Rymo_18 Dec 07 '15 at 19:02
  • @Rymo_18 Normally it should since it is the same reference that is used. – Yassin Hajaj Dec 07 '15 at 23:31

0 Answers0