为什么当我尝试在 PrintWriter 上写入时会抛出异常?

发布于 2024-11-02 02:14:38 字数 3959 浏览 0 评论 0原文

我有一个简单的 ChatClient,它将数据发送到服务器。为了让聊天客户端发送数据,我创建一个 Socket,为 socket.getOutputStream() 创建一个 PrintWriter,然后执行 writer.println("")。由于某种原因,我得到了一个巨大的异常。我查看了与我类似的异常,很多人都说更新java。但我的 Mac 上有最新版本。是因为我有Leopard,还是我的程序粗制滥造?请看一下。

private void setUpNetworking() {

        try {
            **sock = new Socket("127.0.0.1", 65534);**
            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);

            **writer = new PrintWriter(sock.getOutputStream());**
            System.out.println("made connection... everything here works");

        }
        catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("setup fail, but i didn't get this");

        }
    }

    public class SendButtonListener implements ActionListener{
        public void actionPerformed (ActionEvent ev){
            try {
                **writer.println(entertext.getText());**
                **writer.flush();**
                System.out.println("PROBLEM IS RIGHT HERE!!!");

            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("i get a exception. HELP");

            }

这是我的例外:

java.lang.NullPointerException
    at SimpleChatClient$SendButtonListener.actionPerformed(SimpleChatClient.java:52)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:246)
    at java.awt.Component.processMouseEvent(Component.java:5617)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
    at java.awt.Component.processEvent(Component.java:5382)
    at java.awt.Container.processEvent(Container.java:2010)
    at java.awt.Component.dispatchEventImpl(Component.java:4083)
    at java.awt.Container.dispatchEventImpl(Container.java:2068)
    at java.awt.Component.dispatchEvent(Component.java:3918)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
    at java.awt.Container.dispatchEventImpl(Container.java:2054)
    at java.awt.Window.dispatchEventImpl(Window.java:1801)
    at java.awt.Component.dispatchEvent(Component.java:3918)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:501)
    at java.awt.EventQueue.access$000(EventQueue.java:80)
    at java.awt.EventQueue$1.run(EventQueue.java:462)
    at java.awt.EventQueue$1.run(EventQueue.java:461)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:95)
    at java.awt.EventQueue$2.run(EventQueue.java:476)
    at java.awt.EventQueue$2.run(EventQueue.java:475)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:473)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
    **at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)**

我不知道这意味着什么。请帮忙。

I have a simple ChatClient that sends data to a Server. In order for Chat Client to send data, I make a Socket, make a PrintWriter to the socket.getOutputStream(), then do writer.println(""). For some reason, I get the a big fat exception. I looked at similar exceptions to mine, and a lot of people said to update java. But I have the latest version on my Mac. Is it because I have Leopard, or is it my shoddy programming? Please take a look.

private void setUpNetworking() {

        try {
            **sock = new Socket("127.0.0.1", 65534);**
            InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(streamReader);

            **writer = new PrintWriter(sock.getOutputStream());**
            System.out.println("made connection... everything here works");

        }
        catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("setup fail, but i didn't get this");

        }
    }

    public class SendButtonListener implements ActionListener{
        public void actionPerformed (ActionEvent ev){
            try {
                **writer.println(entertext.getText());**
                **writer.flush();**
                System.out.println("PROBLEM IS RIGHT HERE!!!");

            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("i get a exception. HELP");

            }

Here's my exception:

java.lang.NullPointerException
    at SimpleChatClient$SendButtonListener.actionPerformed(SimpleChatClient.java:52)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:246)
    at java.awt.Component.processMouseEvent(Component.java:5617)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
    at java.awt.Component.processEvent(Component.java:5382)
    at java.awt.Container.processEvent(Container.java:2010)
    at java.awt.Component.dispatchEventImpl(Component.java:4083)
    at java.awt.Container.dispatchEventImpl(Container.java:2068)
    at java.awt.Component.dispatchEvent(Component.java:3918)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
    at java.awt.Container.dispatchEventImpl(Container.java:2054)
    at java.awt.Window.dispatchEventImpl(Window.java:1801)
    at java.awt.Component.dispatchEvent(Component.java:3918)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:501)
    at java.awt.EventQueue.access$000(EventQueue.java:80)
    at java.awt.EventQueue$1.run(EventQueue.java:462)
    at java.awt.EventQueue$1.run(EventQueue.java:461)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:95)
    at java.awt.EventQueue$2.run(EventQueue.java:476)
    at java.awt.EventQueue$2.run(EventQueue.java:475)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:84)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:473)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
    **at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)**

I don't know what this means. Please help.

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

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

发布评论

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

评论(3

停滞 2024-11-09 02:14:38

从堆栈跟踪和注释来看,听起来像是当您调用

writer.println(entertext.getText());

SendButtonListeneractionPerformed 方法时,或者 writer enterText 为空。

哪个是 null,以及它是如何以这种方式获得的,从代码中尚不清楚,但您应该能够通过调试器或日志记录来找出它。

From your stack trace and notes, it sounds like when you call

writer.println(entertext.getText());

in the actionPerformed method of your SendButtonListener, either writer enterText is null.

Which is null, and how it got that way isn't clear from the code, but you should be able to figure it out with a debugger or logging.

夏有森光若流苏 2024-11-09 02:14:38

以下是如何调试它 - 要么:

  • 放入一个单独的 println 来打印每个可能为空的东西,就在它出现异常之前,
  • 或者在调试器中运行它(例如 Eclipse 提供了一个)并且当你得到该行时看看变量的值是什么

Here's how to debug this - either:

  • Put in an individual println to print each thing that might be null, just before it has the exception
  • or run it in a debugger (Eclipse provides one, for example) and see what the values of the variables are when you get that line
檐上三寸雪 2024-11-09 02:14:38

Chekc.SimpleChatClient的第52行,有ir.null的东西

Chekc the line 52 of.SimpleChatClient, something there ir.null

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