2

I am getting a NullPointerException 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!

2 Answers2

0

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks, but my project isn't at the point where I can update the JVM without a large scale regression and integration test activity. You couldn't have known that though =) –  Jul 13 '11 at 08:28
  • I am not suggesting you do a large scale regression test. I am suggesting you test this one issue. Only then if you determine this would fix the problem do you consider doing a proper upgrade. ;) – Peter Lawrey Jul 13 '11 at 10:00
0

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • It does appear relevant, except that I am not using JMF. I can't find any bug reports on the JDK for this behaviour, which suggests to me that it is not fixed. The bug evaluation suggests catching the NPE so I guess that is my best choice. –  Jul 13 '11 at 08:15