Question

I have a problem which i am trying to figure out for days now.

Simple Code Overview(PseudoCode):

1 - When a user connects create a PrintWriter for him.

2 - Store the PrintWriter object into a PrintWriter[] array.

3 - Increment the number of users connected.

What is my problem?

  • The compiler throws a NullPointerException

What caused the NPE?

  • When the sendMessage() method is called by object created in the mainWindow class the PrintWriter array if empty, i don't know what is causing it to be empty since all the the variables and arrays have static modifiers and the PrintWriter[] array gets a value in the constuctor(when a user connects).

  • Inside the ServerHandler constuctor when i try to send a message to the client it works fine and i can see that the PrintWriter[] array is not empty, but when i call the sendMessage() method from the mainWindow instance the array is empty and the userCounter integer variable is still 0.

What have i tried to fix it?

  • Setting the variebles and the array modifiers to volatile and static.
  • Writing all the code from scratch over and over again.

Server Class(works fine):

package homeControl;

import java.net.*;
import java.util.Scanner;
import java.io.*;
import java.awt.*;

public class Server{
String sporocilo = null;
int id = 0;

outputHelper o = new outputHelper();


public void sprejmiPovezavo()
{
    try
    {
        ServerSocket svrSock = new ServerSocket(5000);

        while(true){

        Socket klientSocket = svrSock.accept();


        new Thread(new ServerHandler(klientSocket)).start();

        }


    }catch(IOException ex)
    {
        ex.printStackTrace();
    }
}


    public static void main(String[] args){
Server svr = new Server();
svr.sprejmiPovezavo();
}

}

ServerHandler class(this class causes the problems)

package homeControl;

import java.net.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.*;

public class ServerHandler implements Runnable{
static PrintWriter pw;
static PrintWriter[] writerHolder = new PrintWriter[10];
static int userCounter;
static int selectedID;


public ServerHandler(Socket socket){
    try{
        pw = new PrintWriter(socket.getOutputStream());
        writerHolder[userCounter] = pw;

        // Testing if this writerHolder[] can send the message to the client.
        writerHolder[userCounter].println("autoSent - Testint!");
        writerHolder[userCounter].flush();

        userCounter++;// Increment the number of people connected

        //Testing the values of the variables.
        System.out.println("ORG!\nuserCounter:" + userCounter +  "\nwriterHolder[userCounter]" + writerHolder[0]);
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
}
public ServerHandler()
{
    // Empty constructor
    // Used so that i don't need to pass a Socket into the class when i create a ServerHandler object from the mainWindow.class...
}



public void sendMessage(int tempID, String messageToBeSent)
{   // Testing the values of variables at the time that this method gets called.
    System.out.println("tempID:" + tempID + "\n" + "messageToBeSent:" +       messageToBeSent + "\n" + "userCounter:" + userCounter + "\n" + "selectedID:" + selectedID); 
    // End of test/Actual code:
    writerHolder[tempID].println(messageToBeSent);
    writerHolder[tempID].flush();
}

public void run()
{

}

}

mainWindows class(where the sendMessage() method gets called)

package homeControl;

import java.awt.*;
import java.io.*;
import java.net.Socket;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JButton;

public class mainWindow extends JFrame {

private JPanel contentPane;
private JTextField messageTextField;

ServerHandler svr = new ServerHandler(); // Thats why i use the empty constuctor in ServerHandler.class.
outputHelper o = new outputHelper();
int ID;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mainWindow frame = new mainWindow();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public mainWindow() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnOptions = new JMenu("Options");
    menuBar.add(mnOptions);

    JMenu mnId = new JMenu("ID");
    menuBar.add(mnId);

    JMenuItem idEnaItem = new JMenuItem("ID: 0");
    mnId.add(idEnaItem);

    JMenuItem idDvaItem = new JMenuItem("ID: 1");
    mnId.add(idDvaItem);

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    messageTextField = new JTextField();
    messageTextField.setBounds(10, 209, 292, 20);
    contentPane.add(messageTextField);
    messageTextField.setColumns(10);

    JButton sendButton = new JButton("Send");
    sendButton.setBounds(312, 208, 89, 23);
    contentPane.add(sendButton);
    sendButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){

            if(event.getActionCommand() != null)
            { // Calling the sendMessage() in the ServerHandler.class which should send a message to the Client.
               svr.sendMessage(ID,messageTextField.getText());
            }

        }
    });
}
}

Output from the ServerHandler constructor when i'm testing the PrintWriters:

ORG!

userCounter:1

writerHolder[userCounter]java.io.PrintWriter@798c668c

Ouput from the SendMessage method when i manualy try to send a message:

messageToBeSent:dsda

userCounter:0

selectedID:0

ERROR:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at homeControl.ServerHandler.sendMessage(ServerHandler.java:45)
at homeControl.mainWindow$2.actionPerformed(mainWindow.java:84)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Was it helpful?

Solution

As far as I can tell, this is the problem (in Server):

new Thread(new ServerHandler(klientSocket)).start();

Here you're creating a new ServerHandler. That's completely separate from the instance of ServerHandler which you're creating in mainWindow. So the one created by Server will end up with a populated PrintWriter array... and the one that you're creating in mainWindow will always have an array of null element values.

You should have one instance of ServerHandler which both mainWindow and Server share.

OTHER TIPS

In your mainWindow() class, you have

ServerHandler svr = new ServerHandler(); // Thats why i use the empty constuctor in ServerHandler.class.

which calls the empty constructor which doesn't set any PrintWriter instances in the PrintWriter[].

public ServerHandler()
{
    // you don't initialize any array elements
}

You then try to sendMessage() by accessing

writerHolder[tempID].println(messageToBeSent);
writerHolder[tempID].flush();

an unitialized element in the array, causing NPE.


Also, your mainWindow and Server classes are completely disjoint. There's no relation between them. Should there be?

I suggest you start the mainWindow from server:

public void sprejmiPovezavo()
{
    try
    {
        ServerSocket svrSock = new ServerSocket(5000);
        MainWindow mainWindow frame = new mainWindow();
        frame.setVisible(true);

        while(true){

        Socket klientSocket = svrSock.accept();


        new Thread(new ServerHandler(klientSocket)).start();

        }


    }catch(IOException ex)
    {
        ex.printStackTrace();
    }
}

and launch server (sprejmiPovezavo) only, instead of launching mainWindow and the server (sprejmiPovezavo).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top