0

Hello I am using AndroidAsync . if I try to send a message to the WebSocket and the connection drops, I get a FATAL EXCEPTION

    03-20 11:54:53.064: E/AndroidRuntime(13805): FATAL EXCEPTION: AsyncServer
    03-20 11:54:53.064: E/AndroidRuntime(13805): Process: com.example.playerappv2, PID: 13805
    03-20 11:54:53.064: E/AndroidRuntime(13805): java.lang.RuntimeException: java.lang.NullPointerException
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:613)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer.access$4(AsyncServer.java:596)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:552)
    03-20 11:54:53.064: E/AndroidRuntime(13805): Caused by: java.lang.NullPointerException
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.example.playerappv2.core.WebSocketServices$1.onCompleted(WebSocketServices.java:128)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.http.AsyncHttpClient$8.onConnectCompleted(AsyncHttpClient.java:581)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.http.AsyncHttpClient.reportConnectedCompleted(AsyncHttpClient.java:122)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.http.AsyncHttpClient.access$1(AsyncHttpClient.java:110)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.http.AsyncHttpClient$3.onConnectCompleted(AsyncHttpClient.java:225)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer$7.onCompleted(AsyncServer.java:377)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer$7.onCompleted(AsyncServer.java:1)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:96)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:130)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:113)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.TransformFuture.error(TransformFuture.java:22)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.TransformFuture.onCompleted(TransformFuture.java:9)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:96)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:130)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer$8$2.run(AsyncServer.java:412)
    03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:686)
    03-20 11:54:53.064: E/Andr

oidRuntime(13805):  at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:697)
03-20 11:54:53.064: E/AndroidRuntime(13805):    at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:607)
03-20 11:54:53.064: E/AndroidRuntime(13805):    ... 2 more

Does anyone know how to catch this, and dont allow the app to crash ? Here is the same code where the Log refer to the crash line ................

private void connectToPASocket(String SOCKET_ADDR) {
    Log.i(TAG, "connectToPASocket()");

    // Checking
    if (socket != null && socket.isOpen()) return;


    // Initiate web socket connection
    AsyncHttpClient.getDefaultInstance().websocket(SOCKET_ADDR, null, new AsyncHttpClient.WebSocketConnectCallback() {
                @Override
                public void onCompleted(Exception ex, WebSocket webSocket) {
                    Log.i(TAG, "onCompleted");
                    socket = webSocket;


                    if (ex != null) {
                        Log.e(TAG, "onCompleted > if (ex != null)");
                        ex.printStackTrace();
                        return;
                    }
                    //HERE CRASH refered too
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • show code, at which line it is crashing. As its throwing NPException, your object in null while trying to access that object's method. – Bharatesh Mar 20 '15 at 04:36
  • The above code looks good. But below `if` condition are you using socket object to accessing its functions? – Bharatesh Mar 20 '15 at 05:21

0 Answers0