开发者

When does getInputStream() return null?

When we call Socket.getInputStream() the second time after creating cli开发者_JAVA技巧ent it returns null. What can I do?


It will feel like a returned null value if you've used this pattern by accident:

InputStream is = null;
try {
   is = socket.getInputStream();
} catch (IOException e) {
  // TODO will be logged once logging is implemented...
  // e.printStackTrace();
}
is.read(); //  <-- NPE in case IOException...

Double check your code, this could be the reason for your NPE.


According to its documentation Socket.getInputStream() can never return null. It either returns an InputStream or it throws an IOException.

If it throws an IOException then you either closed the socket, the other side closed the socket or some other problem occurred during communication.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜