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.
精彩评论