Search code examples
androidnullpointerexceptionwebsocketautobahn

Android - Autobahn Websocket sending message error(NullPointerException)


I am working on websocket communication with Autobahn. On the main.class of my app, I set to call 'connect()' when users click a button.

// Toggle Button event
    tButton = (ToggleButton) findViewById(R.id.toggleButton1);    
    tButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if(isChecked){

            }else{

            }

        }
    });

And after that, there is MyOffers.class, and if this class is accessed, MyOffers_Fragment class is produced four times automatically, because MyOffers.class contains 'carousel view' and there are four products.

On 'MyOffers_Fragment' class, when users click one of the image of products, message should be sent.

if (pos == 0) {
    product_photo.setImageResource(R.drawable.myoffers_0);
    product_photo.setOnClickListener(new ImageButton.OnClickListener(){
        public void onClick(View v){
            String id = "Product0";
            Log.d(TAG, "Current product is : " + id);
            A.sendMessage(id);  
        }
    });
}

But 'mConnection.sendTextMessage(id1);' this line makes 'NullPointerException' error. There is a class 'Websocket_Connector.class'

public class WebSocket_Connector {

    private static final String TAG = "ECHOCLIENT";
    public final WebSocketConnection mConnection = new WebSocketConnection();

    public void connect(final String wsuri) {

          Log.d(TAG, "Connecting to: " + wsuri); 

          try {
             mConnection.connect(wsuri, new WebSocketHandler() {

                @Override
                public void onOpen() {
                   Log.d(TAG, "Status: Connected to " + wsuri ); 
                   Log.d(TAG, "Connection successful!\n");
                }

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

                @Override
                public void onClose(int code, String reason) {
                   Log.d(TAG, "Connection closed.");
                }
             });
          } catch (WebSocketException e) {

             Log.d(TAG, e.toString());
          }

     public void sendMessage(String message) {
        connect("ws://192.168.x.xxx:xxxx");
        mConnection.sendTextMessage(message); 
     }

 }

I called 'connect()' in the main page class, and after that try to send message. But, it's not working..Can you please help me out?


Solution

  • public class WebSocket_Connector {
    
    private static final String TAG = "ECHOCLIENT";
    public final WebSocketConnection mConnection = new WebSocketConnection();
    private String tmpString = "";
    public void connect(final String wsuri) {
    
          Log.d(TAG, "Connecting to: " + wsuri); 
    
          try {
             mConnection.connect(wsuri, new WebSocketHandler() {
    
                @Override
                public void onOpen() {
                   Log.d(TAG, "Status: Connected to " + wsuri ); 
                   Log.d(TAG, "Connection successful!\n");
                   mConnection.sendTextMessage(tmpString); 
                   tmpString = "";
                }
    
                @Override
                public void onTextMessage(String payload) {
                   Log.d(TAG, "Got echo: " + payload);
                }
    
                @Override
                public void onClose(int code, String reason) {
                   Log.d(TAG, "Connection closed.");
                }
             });
          } catch (WebSocketException e) {
    
             Log.d(TAG, e.toString());
          }
    
     public void sendMessage(String message) {
    
        if (mConnection.isConnected()) 
                mConnection.sendTextMessage(message); 
        else {
           tmpString = message;
           connect("ws://192.168.x.xxx:xxxx");
        }
    
     }
    
     }