0

I am getting "Exception in thread "Thread-2" java.lang.NullPointerException at client.Client.run(Client.java:32)" line 32 is the switch statement line below. The switch is on a enum method. I think the error is a from one of the threads going faster than the other but i'm unsure... I hope i've provided enough code here if not I will add more let me know what I need to add.

public void run()    {
            while (running) {
                try {
                    switch (receiveMessage()) {
                        case SERVER_HELLO:
                            expectingServerHello = true;
                            break;
                            //other cases
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

public ServerMessages receiveMessage() throws IOException {
        String sentence;
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence = inFromServer.readLine();
        return MessageSerialize.fromServerMessage(sentence);
    }
4

1 回答 1

0

如果错误出现在该 switch 行,那么明显的原因似乎是您正在执行NULL,switch (null)也就是您的receiveMessage()返回 NULL,而您没想到会这样(因为您已经在 switch 中找到了它。

要么检查它是否存在NULL,然后在这种情况下做任何你想做的事情(例外,什么都没有,依赖:)),或者修复该函数,使其不再返回NULL..

于 2013-09-04T12:57:55.453 回答