sacOO7 / socketcluster-android-demo

Sample chat app demo built using socketcluster framework in node.js
MIT License
12 stars 2 forks source link

NullPointerException #2

Closed Hayk985 closed 6 years ago

Hayk985 commented 6 years ago

Hy, I cloned the project and it gives NullPointerException in line socket.getChannelByName("MyClassroom").publish(object); . Can you help me, what to do?

sacOO7 commented 6 years ago

Hi @Hayko985 , I will close the issue if your problem is resolved. Please let me know about it.

Hayk985 commented 6 years ago

Hi @sacOO7 )) Really I already forgot about this))) haha . Sure you can close.

sacOO7 commented 6 years ago

Hey no problem. Feel free to raise issues on official repo.

mahima-ms commented 5 years ago

@Hayko985 I too am facing the same issue. Could you please let me know what was the resolution in your case? I was able to run the server successfully on localhost:8000 but I seem to get the error: Got connect error com.neovisionaries.ws.client.WebSocketException: Failed to connect to 'localhost:8000': failed to connect to localhost/127.0.0.1 (port 8000) from /127.0.0.1 (port 46998) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused). I believe since the connection is not successful, the channel isn't created and hence getting an NPE for the result of getChannelByName.

Hayk985 commented 5 years ago

@mahima-ms Hi. My problem was, because getChannelByName() throw NullPointerException. The problem was that the channel sometime not created and even when I checked if (getChannelByName != null), that throw NullPointerException too. The only solution was, that I add the getChannelByName() in try-catch block. That helped me.

Something like this.

try{
    socket.getChannelByName("MyClassroom").publish(object);
} catch (NullPointerException n) {
    n.printStackTrace();
}

So the next question is why my channel become null? Ok let me explain. Sometimes, when screen gone off on phone or connection closed, or something else - socket disconnected after sometime, because when screen goes off, application calls onStop (as I remember) and socket disconnected, after this my connection become null. I think if I'll add my Socket in Background using WorkManager, this problem will be solved))

mahima-ms commented 5 years ago

Awesome! Understood. Thank you very much @Hayko985 for such a detailed explanation on the issue and sharing the resolution. Appreciate your time :)

Hayk985 commented 5 years ago

@mahima-ms Thank you too. Good luck :)