Vicky
Vicky

Reputation: 1532

Coap Example For Observe is not working in Eclipse

I am working on COAP for transfer information between devices. I am only in learning stage.I referred a document which have implemented the COAP sample in eclipse by using californium. In that, I tried the example provided in the document to observe the server states continuously, but it is not working in it just shows null pointer exception while I am running the project. Then I tried to debug the code for what is the reason for null pointer exception, it is running good its getting the result continuously, I don't understand why it is producing error during running the project but during debug its not. Please help

The code I am using is:

public class HelloObserver {

    public static void main(String[] args) {
         CoapClient client = new CoapClient("coap://vs0.inf.ethz.ch:5683/obs");

         CoapObserveRelation relation=    client.observe(new CoapHandler() {


                @Override
                public void onLoad(CoapResponse response) {

                    System.out.println(response.getResponseText());
                    /*response.notify();*/
                    // TODO Auto-generated method stub

                }

                @Override
                public void onError() {
                    System.err.println();
                    // TODO Auto-generated method stub

                }
            });

         relation.proactiveCancel();
    }

}

Upvotes: 1

Views: 1451

Answers (1)

kartben
kartben

Reputation: 2535

Vicky, your main() as it is just exits pretty much immediately, therefore there is no way the CoapHandler is going to be called. After you have setup your observe relation you need to make sure your program is still doing something, e.g. a while(true) where you sleep for 10 secs and send a ping, or something like that.

You may want to have a look at how it's done in this example where essentially the program terminates when a key is pressed.

Upvotes: 3

Related Questions