为什么它返回空指针异常(服务器端)

发布于 2024-08-19 00:47:11 字数 1934 浏览 2 评论 0原文

这是我的服务器类,它允许客户端相互聊天,但它将返回此行的空指针异常: while (!(line = in.readLine()).equalsIgnoreCase("/quit")) 你能帮我吗?谢谢。

我的 ChatHandler 类:

 final static Vector handlers = new Vector(10);
private Socket socket;
private BufferedReader in;
private PrintWriter out;

public ChatHandler(Socket socket) throws IOException {
    this.socket = socket;
    in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(
            new OutputStreamWriter(socket.getOutputStream()));
}

@Override
public void run() {
    String line;

    synchronized (handlers) {
        handlers.addElement(this);
    // add() not found in Vector class
    }
    try {
        while (!(line = in.readLine()).equalsIgnoreCase("/quit")) {
            for (int i = 0; i < handlers.size(); i++) {
                synchronized (handlers) {
                    ChatHandler handler =
                            (ChatHandler) handlers.elementAt(i);
                    handler.out.println(line + "\r");
                    handler.out.flush();
                }
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            synchronized (handlers) {
                handlers.removeElement(this);
            }
        }
    }
}

客户端类的一部分:

  String teXt = MainClient.getText();

    os.println(teXt);
    os.flush();
    try {
        String line = is.readLine();



            setFromServertext("Text recieved:"+line+"\n");

        is.close();
        is.close();
        c.close();
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }

this is my server class which let the clients to chat with each other but it will return nullpointer exception for this line: while (!(line = in.readLine()).equalsIgnoreCase("/quit")) would you please help me?thanks.

my ChatHandler class:

 final static Vector handlers = new Vector(10);
private Socket socket;
private BufferedReader in;
private PrintWriter out;

public ChatHandler(Socket socket) throws IOException {
    this.socket = socket;
    in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(
            new OutputStreamWriter(socket.getOutputStream()));
}

@Override
public void run() {
    String line;

    synchronized (handlers) {
        handlers.addElement(this);
    // add() not found in Vector class
    }
    try {
        while (!(line = in.readLine()).equalsIgnoreCase("/quit")) {
            for (int i = 0; i < handlers.size(); i++) {
                synchronized (handlers) {
                    ChatHandler handler =
                            (ChatHandler) handlers.elementAt(i);
                    handler.out.println(line + "\r");
                    handler.out.flush();
                }
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            synchronized (handlers) {
                handlers.removeElement(this);
            }
        }
    }
}

a part of client class:

  String teXt = MainClient.getText();

    os.println(teXt);
    os.flush();
    try {
        String line = is.readLine();



            setFromServertext("Text recieved:"+line+"\n");

        is.close();
        is.close();
        c.close();
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2024-08-26 00:47:11

这不是正确的习惯用法。当到达流末尾时,BufferedReader#readLine() 将返回 null

因此,以下内容

while (!(line = in.readLine()).equalsIgnoreCase("/quit")) {
    // Do stuff.
}

必须替换为:

while ((line = in.readLine()) != null && !line.equalsIgnoreCase("/quit")) {
    // Do stuff.
}

另请参阅 Sun 自己的基本 Java IO 教程如何使用 BufferedReaderhttp://java.sun.com/docs/books/tutorial/essential/io/

This isn't the correct idiom.The BufferedReader#readLine() will return null when end of stream is reached.

Thus, the following

while (!(line = in.readLine()).equalsIgnoreCase("/quit")) {
    // Do stuff.
}

has to be replaced by:

while ((line = in.readLine()) != null && !line.equalsIgnoreCase("/quit")) {
    // Do stuff.
}

Also see Sun's own basic Java IO tutorials how to use a BufferedReader: http://java.sun.com/docs/books/tutorial/essential/io/

梦幻的味道 2024-08-26 00:47:11

当没有更多内容可供读取时,in.readLine() 将返回 null。你需要将其更改为

String line;
while ((line = in.readLine()) != null) {
    if (!line.equalsIgnoreCase("/quit")) {

    }
}

in.readLine() will return null when there is nothing more to read. You need to change that to

String line;
while ((line = in.readLine()) != null) {
    if (!line.equalsIgnoreCase("/quit")) {

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文