开发者

NullPointerException in Thread's run method

I would really appreciate help with my program. It is some sort of chat server with multiple clients. Here's the server code:

package com.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
    public static int PORT;
    private ServerSocket server;
    private Socket socket;

    public Server(int port) throws IOException {
        PORT = port;
        server = new ServerSocket(PORT);
        System.out.println("server started");
        try {
            while (true) {
                socket = server.accept();
                try {
                    new ServeClient(socket);
                } catch (IOException e) {
                    socket.close();
                }
            }
        } finally {
            server.close();
        }

    }

    public static void main(String[] args) throws IOException {
        int port = Integer.parseInt(args[0]);
        Server server = new Server(port);
    }

}

I start the server and then create a Client. The server receives connection socket from socket and creates a ServeClient Thread. Here's ServeClient code:

package com.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Vector;

import com.gui.WindowManager;

public class ServeClient extends Thread {
    private final Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private String msg;
    public static final String ENDSTRING = "END";
    public static Vector clients = new Vector();

    public ServeClient(final Socket so开发者_如何转开发cket) throws IOException {
        this.socket = socket;
        System.out.println("socket " + socket);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                socket.getOutputStream())), true);
        start();
    }

    public void run() {
        try {
            clients.add(this);
            while (true) {
                msg = in.readLine();
                if (msg == ENDSTRING)
                    break;
                broadcast(msg);
            }
            System.out.println("closing...");
        } catch (IOException e) {
            System.err.println("IO EXCEPTION");
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                System.err.println("SOCKET NOT CLOSED");
            }
        }
    }

    @SuppressWarnings("deprecation")
    public void broadcast(String msg) {
        synchronized (clients) {
            Enumeration<ServeClient> e = clients.elements();
            while (e.hasMoreElements()) {
                ServeClient serveClient = e.nextElement();
                try {
                    synchronized (serveClient.out) {
                        serveClient.out.println(msg);
                    }
                } catch (Exception eee) {
                    serveClient.stop();
                }
            }
        }
    }
}

What i get is a NullPointerException when ServeClient invokes run() method

server started
socket Socket[addr=/127.0.0.1,port=51438,localport=8888]
Exception in thread "Thread-0" java.lang.NullPointerException
    at com.server.ServeClient.run(ServeClient.java:33)

line 33 is the line with first "try" statement in ServeClient run() method


com.server.ServeClient.run(ServeClient.java:33)

I don't believe that it's happening at the try.

Open up an IDE, turn on debugging, and step through until you can see what's happening. That's the fastest way to figure out what you've missed.

There's an object that you're assuming is fine that is not. Find it.

Here's an example of how to do this properly:

http://www.kodejava.org/examples/216.html


Your problem is with the order in which static instance variables are initialised. Try doing something like:

...
private static Vector clients = null;
...
if (clients==null) {
    clients = new Vector(); // consider putting this in a synchronized block
}

before you add the client to the vector.


Sorry for necroing such an old issue but it seemed like this problem wasn't resolved, so I'll give a bit of input from my end.

I've had a similar problem and the compiler also kept telling me that the problem was at the start() method. However, when I commented out the thread part and just ran the code on the same thread as the UI, the compiler directed me to the real source of the problem: the code inside the thread.

After making sure the code didn't give an error, I enclosed the code with the original thread code, and it stopped giving me the NullPointerException error.

Hope this helps someone along the way.


Remove the duplicate class declaration in JPanel.

I was trying to run a timer thread that updated a clock in the main application window.

I had created the JFrame with Eclipse/WindowBuilder and had followed a tutorial on how to make a timer. I had copied the declaration of the textfield into the class declaration to make it available for the entire class, but forgot to remove the Class Id in front of the widget definition. So it still initialized the local instance and not the global one. Thus when I accessed the global one it was still null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜