3

I am working on websocket communication. From android device(client) to linux-based PC(server). I succeeded to connect websocket to server. But the problem is I got fail with sending a data(string value)

There is a carousel view with four products. So, when I click photo of product0, I set string as "product0" and send this string value to server. I am using Autobahn library.

The code is here

import de.tavendo.autobahn.WebSocketConnection;

public class Myoffers_Fragment extends Fragment {

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

    public static Fragment newInstance(Myoffers context, int pos, float scale)
    {
        Bundle b = new Bundle();
        b.putInt("pos", pos);
        b.putFloat("scale", scale);
        return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        LinearLayout l = (LinearLayout) inflater.inflate(R.layout.mf, container, false);

        int pos = this.getArguments().getInt("pos");
        TextView tv = (TextView) l.findViewById(R.id.text);
        tv.setText("Product " + pos);



        ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);


        if (pos == 0) {
            product_photo.setImageResource(R.drawable.myoffers_0);
            product_photo.setOnClickListener(new ImageButton.OnClickListener(){
                public void onClick(View v){
                    String id1 = "Product0";
                    Log.d(TAG, "Current product is : " + id1);
                    mConnection.sendTextMessage(id1);
                    Log.d(TAG, id1 + "is sent to server!");
                }
            });
        }

Is it possible that "extends Fragment" makes the error?.. Error is occurred like below..

06-19 12:02:01.310: E/AndroidRuntime(2712): FATAL EXCEPTION: main
06-19 12:02:01.310: E/AndroidRuntime(2712): java.lang.NullPointerException
06-19 12:02:01.310: E/AndroidRuntime(2712):     at de.tavendo.autobahn.WebSocketConnection.sendTextMessage(WebSocketConnection.java:137)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at com.example.philips.Myoffers_Fragment$1.onClick(Myoffers_Fragment.java:56)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at android.view.View.performClick(View.java:3511)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at android.view.View$PerformClick.run(View.java:14105)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at android.os.Handler.handleCallback(Handler.java:605)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at android.os.Looper.loop(Looper.java:137)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at android.app.ActivityThread.main(ActivityThread.java:4446)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 12:02:01.310: E/AndroidRuntime(2712):     at dalvik.system.NativeStart.main(Native Method)

The error occurred when I click the photo to send string value.

user2500696
  • 65
  • 1
  • 7
  • Where do you actually connect using `mConnection`? – Ken Wolf Jun 19 '13 at 10:35
  • Last line of `newInstance()` should be replaced with: `Myoffers_Fragment result = new Myoffers_Fragment(); result.setArguments(b); return result;` ... as you seem to return a fragment with empty arguments list. Also, the error is thrown in `WebSocketConnection.sendTextMessage()`. Can you post that code? – gunar Jun 19 '13 at 10:38
  • @gunar Which code do you want me to post?..Myoffers_Fragment is the code aboove – user2500696 Jun 19 '13 at 10:44
  • Where can one find the class `WebSocketConnection`? I can't find it in Android packages. Did you use a library or is it something you wrote? – gunar Jun 19 '13 at 11:12
  • @gunar WebSocketConnection is included in Autobahn library – user2500696 Jun 19 '13 at 11:36
  • @gunar check this out. 'http://autobahn.ws/static/reference/android/_web_socket_connection_8java_source.html' – user2500696 Jun 19 '13 at 11:37
  • Then Ken's answer seems the valid one. – gunar Jun 19 '13 at 11:41
  • @gunar no, the answer is not valid yet! I called'connect()' in the main page – user2500696 Jun 19 '13 at 12:30
  • What do you mean with **called connect in the main page**? Do you have multiple instances of `WebSocketConnection`? – gunar Jun 19 '13 at 12:38
  • @gunar Ken said that I need to call "connect()" before using "mConnection". But, in other class, calls "connect()". You can check this detail question here "http://stackoverflow.com/questions/17190843/android-autobahn-websocket-sending-message-errornullpointerexception" – user2500696 Jun 19 '13 at 12:41
  • Of course: those are 2 distinct instances (you have a WebSocketConnection instance in main page and a different instance in Fragment) ... no wonder you're getting those NPEs – gunar Jun 19 '13 at 12:45
  • Then...how can I fix it..? There is on/off button to connect/disconnect server and I need to send a message on another class. – user2500696 Jun 19 '13 at 12:49
  • The simplest solution: set the connector as singleton. The activity would call: `Connector.getInstance().connect()`. From fragment: `Connector.getInstance().sendMessage(...)` – gunar Jun 19 '13 at 12:50
  • Just don't forget to close that connector with `Connector.getInstance.finish()`. Maybe best suited in `Activity.finish()`; – gunar Jun 19 '13 at 12:58
  • In the main.class I changed "A.connect("ws://xxx");" to "A.getInstance().connect("ws://192.168.3.100:7681");" But it makes error on getInstance() – user2500696 Jun 19 '13 at 13:03

1 Answers1

2

The error is happening in your onClick here:

mConnection.sendTextMessage(id1);

Looks like you have declared mConnection at the top, but not made any connection.

Looking at the docs, you need to call .connect() on your mConnection before using it.

Line 137 of WebSocketConnection.java is:

public void sendTextMessage(String payload) {
    mWriter.forward(new WebSocketMessage.TextMessage(payload));
}

mWriter is null until you call .connect(). Source code

So, make sure you have a valid connection (by calling .connect()) before you use the mConnection object.

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Then, where should I call .connect() on my code. Because in the Myoffers_Fragment.class will be produced 4 of it when I access carousel view. – user2500696 Jun 19 '13 at 10:46
  • Without knowing exactly what your architecture is and what you're trying to do, it's hard to say. Personally if I had a need to access this connection a lot I would let my Activity control it, not various Fragments. – Ken Wolf Jun 19 '13 at 10:48
  • And actually, I made Websocket_connector.class and in that code, I declared 'public void connect(final String wsuri)' – user2500696 Jun 19 '13 at 10:50
  • Well, hopefully I have answered your question. If you have another question about architecture maybe it's better to open another question, or maybe someone else will help. Good luck! – Ken Wolf Jun 19 '13 at 10:51
  • in the main page, I called 'A.connect("ws://192.168.3.100:7681");' like this. And after that, I accessed MyOffers_Frgment.. but same result. do I need to call connect() in the same class? – user2500696 Jun 19 '13 at 12:02