开发者

Why does my PrintWriter throw exceptions when I try to write on it?

开发者 https://www.devze.com 2023-02-25 05:00 出处:网络
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 so

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.


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.


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


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

0

精彩评论

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

关注公众号