0

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 :|

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

1

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks a lot - noob mistake on my part : the NullPointerException has been solved, but somehow I'm not getting it on the other side until I close both the sender and listener.. I'll work it out.. :{ – Haim Rimon Sep 26 '13 at 04:34
  • @HaimRimon Don't forget to call `flush()` on your `OutputStream` instances. – Sotirios Delimanolis Sep 26 '13 at 04:36
  • For closure : I didn't get it on the other side because I used readline, and didn't send a "\n" at the end, so the info didn't arrive until I closed the socket of the output.. :) – Haim Rimon Sep 29 '13 at 06:57