0

I've integrated AutoBahn to take care of my WebSocket needs in my android application. This works well most of the time.

Occasionally I crash out with this log:

java.lang.NullPointerException
            at de.tavendo.autobahn.WebSocketConnection.sendTextMessage(WebSocketConnection.java:137)
            at com.test.testSockets.websockets.Request.GetForceResultsRequest$1.onOpen(GetForceResultsRequest.java:48)
            at de.tavendo.autobahn.WebSocketConnection$1.handleMessage(WebSocketConnection.java:370)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method) 

Here is my GetForceResultRequest Class:

public class GetForceResultsRequest extends BaseRequest {
    protected final WebSocketOptions mWebSocketOptions = new WebSocketOptions();
    private static String m_TAG = GetForceResultsRequest.class.getSimpleName();
    private String mRequest = "{\"request\":\"getForceTestResults\"}";
    private String mSensorId;


    public GetForceResultsRequest(String hostAddress, RemoteCallListener callListener){
        mSocketHostAddress = hostAddress;
        listener = callListener;
        mAutoBahnConnection = new WebSocketConnection();

        mWebSocketOptions.setReceiveTextMessagesRaw(true);
        mWebSocketOptions.setSocketReceiveTimeout(100000);
        mWebSocketOptions.setSocketConnectTimeout(100000);

        //mWebSocketOptions.setTcpNoDelay(true);

        Log.i(m_TAG, mRequest);
    }

    @Override
    protected Void doInBackground(Void... params){

        try {
            mAutoBahnConnection.connect(mSocketHostAddress,new String[]{mProtocol} ,new WebSocketHandler(){

                @Override
                public void onOpen() {
                    Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
                    mAutoBahnConnection.sendTextMessage(mRequest);
                }

                @Override
                public void onTextMessage(String payload) {
                    Log.i(m_TAG, "Got echo: " + payload);
                }

                @Override
                public void onRawTextMessage(byte[] payload) {

                    try {
                        if(payload!=null) {
                            mJsonResponse = new String(payload, "UTF-8");
                            Log.i(m_TAG, mJsonResponse);
                            parseJson(mJsonResponse, GetForceTestResultResponse.class);
                        }

                        //parseJson(mJsonResponse);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        Log.i(m_TAG, e.toString());
                        listener.onRemoteErrorOccur(e);
                    } catch (IOException e) {
                        Log.i(m_TAG, e.toString());
                        listener.onRemoteErrorOccur(e);
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        Log.i(m_TAG, e.toString());
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        Log.i(m_TAG, e.toString());
                        e.printStackTrace();
                    }
                }

                @Override
                public void onBinaryMessage(byte[] payload) {
                    Log.i(m_TAG, "ON BINARY MESSAGE");
                }


                @Override
                public void onClose(int code, String reason) {
                    Log.i(m_TAG, "Connection lost."+ reason);

                }
            }, mWebSocketOptions);
        } catch (WebSocketException e) {
            Log.d(m_TAG, e.toString());
            listener.onRemoteErrorOccur(e.toString());
        }
        return null;
    }
}

I am calling this 5 times a second in a countdown timer. This countdown timer runs for 30 sec

So here is the method

  private boolean forceTestPoll(){

        WebServices.getInstance().getForceTestResults(m_HostAddress, new RemoteCallListener() {
            @Override
            public void onRemoteCallComplete(Object result) {
                GetForceTestResultResponse resultResponse = (GetForceTestResultResponse)result;
                List<SensorForceTestResult> list = resultResponse.getSensors();

                for(SensorForceTestResult forceTestResult: list){
                        if(forceTestResult.getForceResult()==1){
                            mForceDetected = true;
                        }else {
                            mForceDetected = false;
                        }
                }
            }

            @Override
            public void onRemoteErrorOccur(Object error) {
                Log.i(m_Tag, "ERROR: " +(String)error);
            }
        });
        return mForceDetected;}

This is called in the onTick method of countdowntimer which is called 5 times a second.

so forceTestPoll() returns a boolean so it's called like:

if(forceTestPoll()){
//processing here
}

So I would really appreciate it if someone could explain why I get this error on occasion? Am I calling it too frequently?

EDIT

So below is the base request that is extended

public class BaseRequest extends AsyncTask<Void, Void, Void>{

    protected static WebSocketConnection mAutoBahnConnection;
    protected String mSocketHostAddress;
    protected String mJsonResponse;

    protected RemoteCallListener listener;
    protected static final String  mProtocol = "sensor_tester_api_v2";


    @Override
    protected Void doInBackground(Void... params) {return null;}

    @Override
    protected void onPostExecute(Void v){}

    protected void parseJson(String jsonResponse, Object WebSocketsResponse) throws IllegalAccessException, InstantiationException, IOException {
       Object response = WebSocketsResponse;
        response =  new ObjectMapper().readValue(jsonResponse, (Class<Object>) response);
        listener.onRemoteCallComplete(response);
    }
}
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • mAutoBahnConnection is null ... but most of the time is not null... obviously without knowledge about where is mAutoBahnConnection defined it is impossible to guess ... but I would bet that AsyncTask lives longer than the object(activity? fragment?) where is called (orientation changed?) – Selvin Mar 13 '15 at 10:54
  • @Selvin Hi, thanks for the reply. I know why I'm getting the crash, but I don't know why it's null. I have a base request class, in there I define mAutoBahnConnection (I'll update my op to show). I have a fragment in which it is called. The idea is I'm testing sensors for force. So I'm polling 5 times a second. There is a button on this fragment to end this test, if this is clicked the test is ended and the fragment is then added to the backstack. Can I ask your thoughts based on the update on the op – DJ-DOO Mar 13 '15 at 11:13
  • Hi, can anyone offer any further advice on this? – DJ-DOO Mar 13 '15 at 14:55

0 Answers0