开发者

NullPointerException thrown from Socket.getInputStream()

I am getting a NullPointerExceptio开发者_运维百科n raised in this code very rarely. This is part of a video streaming application that consumes multiple RTSP streams simultaneously, and disconnects and reconnects to them regularly (i.e. based on the user).

private void openSocket() throws IOException {
  rtspSocket = new Socket();
  rtspSocket.connect(new InetSocketAddress(ip, port), connectionTimeout);
  rtspSocket.setSoTimeout(readTimeout);

  // NullPointerException on next line
  bufStream = new BufferedInputStream(rtspSocket.getInputStream());
}

The stack trace is as follows:

java.lang.NullPointerException
at java.io.FileInputStream.<init>(Unknown Source)
at java.net.SocketInputStream.<init>(Unknown Source)
at java.net.PlainSocketImpl.getInputStream(Unknown Source)
at java.net.Socket$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.Socket.getInputStream(Unknown Source)
at my.code.shown.above()

My Google-fu has pulled up this bug from Apr 2000 and this bug from Nov 1997 but I am not convinced they are applicable. I have also had a wander through the JDK 1.6 update 7 code that I am building against and noticed that this exception is only raised whenever the FileDescriptor that is contained in the PlainSocketImpl is null. This value is then passed into the FileInputStream constructor from the SocketInputStream constructor.

Short of catching the NPE and throwing an IOException, is there something I can do here? My suspicion is that this is caused by some sort of IOException that sets the FileDescriptor to null. It would be much easier if an IOException was thrown instead!


The first bug appears very relevant. The second one, not so, and it was addressed by adding shutdownOutput() many years ago now.


If you suspect a bug in the JVM I would try Java 6 update 26 rather than update 7. If this fixes the problem its likely to have been a bug.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜