Question

I'm trying to create a simple client/server app in java, it has a main that starts a thread - the listener thread and a runnable - the sender thread. They communicate with a working program. The socket is created on the Listener thread, as are the input and output static variables.

The problem is : I can use the output, but only if I call it from the listener thread where it is defined. (output.writeBytes("0000");) When I try calling it from the Sender runnable, I get a null exception!! (InfoListener.output.writeBytes("0000");)

Here is my (not so smart) code, without all the exception handling :

* InfoListener.java file *

public class InfoListener extends Thread {

    public int port = 5000;
    public Socket socket = null;
    static BufferedReader input;
    static DataOutputStream output;
    static boolean can_start_sender = false;

    static boolean active = true;
    static String answer="";

    public void run() 
    {               
        // init socket
        socket = new Socket("127.0.0.1", port);
        output = new DataOutputStream(socket.getOutputStream());
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        can_start_sender = true;

        while (active) // while main app is active
        {
            // Read new data!!
            answer = input.readLine();
            if (answer!=null)
            {
                System.out.println("Info : Listener received : " + answer);
            }
        }
    }
}

* InfoSender.java file *

public class InfoSender implements Runnable {


    static InfoListener infoListener;
    static InfoSender infoSender;
    static String string_to_send = "0000";

    public static void main(String[] args)
    {
        // start listener
        infoListener = new InfoListener();
        infoListener.start();

        // Start Sender
        infoSender = new InfoSender();
        infoSender.run();

        while (infoListener.isAlive())
            Thread.sleep(100);
    }

    public void run()
    {
        //attempt to connect to the server and send string
        // Wait for socket
        while (InfoListener.can_start_sender = false)
            Thread.sleep(100);

        // write -------- HERE IS THE NULLPOINTEREXCEPTION ------------
        InfoListener.output.writeBytes(string_to_send);
        System.out.println("Info : info sent :"+ string_to_send);

        // wait a bit for listener to get response back, then close
        Thread.sleep(10000);
        InfoListener.active = false; 
        }
}

Please help :|

Was it helpful?

Solution

In

while (InfoListener.can_start_sender = false)

you are assigning false to can_start_sender. The while will therefore always resolve to false.

It's possible that code following the while

// write -------- HERE IS THE NULLPOINTEREXCEPTION ------------
InfoListener.output.writeBytes(string_to_send);

will get executed before the other Thread has time to initialize the static output field thus causing a NullPointerException.

Use

while (!InfoListener.can_start_sender)

or better yet use a CountDownLatch or similar java.util.concurrent lock object. You should also make can_start_sender volatile.

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