Java NullPointerException,请帮我理解原因

时间:2011-05-10 00:34:49

标签: java nullpointerexception

编辑:我遇到的问题是我使用了错误的套接字。它现在已经解决了。

您好,

我正在使用Java中的一个小聊天客户端,其中包括套接字编程。但是我在启动连接时遇到了一些问题,因为我得到了一个N​​ullPointerException并且我无法理解为什么!

这是制造麻烦的代码:

        try {
            sock = new Socket(host, port);
            Connection DaUberConnection = new Connection(sock);
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

用于创建套接字的变量在类级别声明,并且完全按预期工作(我认为)。主机和端口已设置。

Socket sock;
String host;
int port;

据我所知,这与我在某个地方错误的地方抛出一个空的东西有关,但我真的看不出它是如何挂在一起的。

抱歉我的英文不好或在java中的nubieness :) 如果有人能解释为什么会发生这种情况,那将是黄金!

完整错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at clientpackage.Connection.<init>(Connection.java:24)
at clientpackage.Client$1.actionPerformed(Client.java:91)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

我认为问题在于您没有构建和部署问题中的相同代码。

  • 您说此行发生异常:

    in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    

    这意味着sock必须为空。

  • 但是从这里开始调用:

    sock = new Socket(host, port);  // 1
    Connection DaUberConnection = new Connection(sock); // 2
    

    这意味着sock不能为空......除非发生其他事情。

如果您报告的的代码确实,那么这些症状的唯一可能解释是,如果某个其他线程正在将null分配给sock两个语句之间的小窗口(12)。即使假设某些其他线程可以更新sock,这个事件也只会发生在“蓝月亮”中。


返回您的开发环境,保存您正在编辑的所有文件,清除所有“.class”文件,完整版本,然后重新运行您的应用程序。


我认为我发现您的问题...... ssock

这是一个很好的说明,为什么一个类将状态变量暴露给子类是BAD PRACTICE。 sock实例变量应为private,子类应使用getter访问它,并应通过构造函数链初始化它。

睡一觉,然后明天开始修复这个设计缺陷。

答案 1 :(得分:0)

分开第24行。

使用

if (sock==null) System.out.println ("Sock is null");
else {
    is = sock.getInputStream(); 
    if (is==null) System.out.println ("inputStream is null");
    else isr = InputStreamReader(is);
}

并查看哪一个为null。这条线是什么?

Connection DaUberConnection = new Connection(sock);