开发者

When does getInputStream() return null?

开发者 https://www.devze.com 2023-02-14 02:44 出处:网络
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 acciden

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

精彩评论

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

关注公众号