Skip to content

Commit 27203eb

Browse files
fix(Android): prevent NullPointerException in TcpReceiverTask (#227)
1 parent 4c6d55e commit 27203eb

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

android/src/main/java/com/asterinet/react/tcpsocket/TcpSocketClient.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,15 @@ public void setKeepAlive(final boolean enable, final int initialDelay) throws IO
216216
}
217217

218218
public void pause() {
219-
receiverTask.pause();
219+
if (receiverTask != null) {
220+
receiverTask.pause();
221+
}
220222
}
221223

222224
public void resume() {
223-
receiverTask.resume();
225+
if (receiverTask != null) {
226+
receiverTask.resume();
227+
}
224228
}
225229

226230
/**
@@ -246,6 +250,13 @@ public TcpReceiverTask(TcpSocketClient clientSocket, TcpEventListener receiverLi
246250
public void run() {
247251
int socketId = clientSocket.getId();
248252
Socket socket = clientSocket.getSocket();
253+
254+
// Guard against null socket - can happen if destroy() is called
255+
// before the receiver task starts, or if socket creation failed
256+
if (socket == null) {
257+
return;
258+
}
259+
249260
byte[] buffer = new byte[16384];
250261
try {
251262
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
@@ -259,7 +270,7 @@ public void run() {
259270
}
260271
}
261272
} catch (IOException | InterruptedException ioe) {
262-
if (receiverListener != null && !socket.isClosed() && !clientSocket.closed) {
273+
if (receiverListener != null && socket != null && !socket.isClosed() && !clientSocket.closed) {
263274
receiverListener.onError(socketId, ioe);
264275
}
265276
}

0 commit comments

Comments
 (0)