react-native-tcp-socket icon indicating copy to clipboard operation
react-native-tcp-socket copied to clipboard

Attempt to invoke virtual method 'java.io.InputStream java.net.Socket.getInputStream()' on a null object reference

Open sergeushenecz opened this issue 1 year ago • 6 comments

Hello everyone. I see in the sentry sometimes this errors. Maybe someone are faced also with this issues?

sergeushenecz avatar Apr 12 '24 16:04 sergeushenecz

This is a bug(i think) that i "fixed" in the version I forked... i just changed the write() method in TcpSocketClient:

public void write(final int msgId, final byte[] data) {
        writeExecutor.execute(new Runnable() {
            @Override
            public void run() {
               // THIS IS THE FIX VALIDATION ***********************
                if (socket == null) {
                    receiverListener.onError(getId(), new IOException("Attempted to write to closed socket"));
                    return;
                }
                try {
                    socket.getOutputStream().write(data);
                    receiverListener.onWritten(getId(), msgId, null);
                } catch (IOException e) {
                    receiverListener.onWritten(getId(), msgId, e);
                    receiverListener.onError(getId(), e);
                }
            }
        });
    }

marianolc avatar Apr 15 '24 05:04 marianolc

And this code will fix problem? Maybe need to add pull request for it?

sergeushenecz avatar Apr 15 '24 07:04 sergeushenecz

@marianolc I've checked in library and see the same code as you wrote me. I don't understand where need to add fix. image

sergeushenecz avatar Apr 15 '24 16:04 sergeushenecz