Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] NPE thrown by MqttAndroidClient without any object set to null

Hi,

This is a simillar issue.

IMqttToken.getSessionPresent throws NullPointerException
https://github.com/eclipse/paho.mqtt.android/issues/200

IMqttToken, which is an object to return from MqttAndroidClient, is not initialized well. In the case above, IMqttToken.sessionPresent is not initialized when get connected, so if a client tries to access it, it throws NPE.

Yohei Onishi
OGIS-RI Co.,Ltd.
Tel: 050-3367-1634

On 2017/07/20 6:26, Sergio Torassa wrote:
Ian,

I tried different examples found in different forums, but the results are the same.

Surely there is the step I'm missing, as the paho example in github perfectly works (I cloned the project on the same machine, compiled it and installed on the same smartphone).

In any case, whatever the reason for the exception in my code, the NPE should be added in the documentation in the list of exceptions possibly thrown by MqttAndroidClient.publish(...) and a possible reason

Sergio


Il 19 lug 2017 3:43 PM, Ian Craggs <icraggs@xxxxxxxxxxxxxxxxxxxxxxx> ha scritto:

    Sergio,


    James Sutton, who looks after the Android service, has been away
helping IBM at the Wimbledon tennis tournament for a few weeks. I'll ask him to look when he gets back. I'm not familiar with the
    Android client, but are there examples you can look at?


    Ian


    On 19/07/2017 14:34, Sergio Torassa wrote:

        I didn't see any reply to this thread.

        Have anyone had the chance to give a look at what could be
        the reasons for the NPE?



        ------------------------------------------------------------------------
        *Da:* paho-dev-bounces@xxxxxxxxxxx
        <mailto:paho-dev-bounces@xxxxxxxxxxx>
        <paho-dev-bounces@xxxxxxxxxxx>
        <mailto:paho-dev-bounces@xxxxxxxxxxx> per conto di Sergio
        Torassa <sertorassa@xxxxxxxxxxx> <mailto:sertorassa@xxxxxxxxxxx>
        *Inviato:* mercoledì 24 maggio 2017 17:26
        *A:* General development discussions for paho project
        *Oggetto:* Re: [paho-dev] NPE thrown by MqttAndroidClient
        without any object set to null

        Can anyone please help me understanding what's the issue?

        Just to help the reader I include my complete code and the code
        where there is the actual call of a method on a null object in
        MqttAndroidClient.java (outlined in yellow)

        Why mqttService is null?



        ------  MY CODE  --------


        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import org.eclipse.paho.android.service.MqttAndroidClient;
        import org.eclipse.paho.client.mqttv3.IMqttActionListener;
        import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
        import org.eclipse.paho.client.mqttv3.MqttCallback;
        import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
        import org.eclipse.paho.client.mqttv3.MqttException;
        import org.eclipse.paho.client.mqttv3.MqttMessage;
        import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

        public class MainActivity extends AppCompatActivity {

             @Override
             protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_main);

                 String topic        = "MQTT Examples";
                 String content      = "Message from MqttPublishSample";
                 int qos             = 2;
                 String broker       = "tcp://iot.eclipse.org:1883";
                 String clientId     = "JavaSample";
                 MemoryPersistence persistence = new MemoryPersistence();

                 try {
                     MqttAndroidClient sampleClient = new
        MqttAndroidClient(getApplicationContext(),broker,clientId);

                     MqttConnectOptions connOpts = new MqttConnectOptions();
                     connOpts.setCleanSession(true);

                     sampleClient.setCallback(new MqttCallback() {...} );

                     sampleClient.connect(connOpts);

                     MqttMessage message = new
        MqttMessage(content.getBytes());
                     message.setQos(qos);

                     sampleClient.publish(topic, message);

                     System.exit(0);

                 } catch(MqttException me) {

                     me.printStackTrace();

                 } catch (Exception e) {

                     e.printStackTrace();
                     throw new RuntimeException(e);

                 }
             }
        }

        -------   MqttAndroidClient.java    ---------

        @Override
        public IMqttDeliveryToken publish(String topic, MqttMessage message,
               Object userContext, IMqttActionListener callback)
               throws MqttException, MqttPersistenceException {
            MqttDeliveryTokenAndroid token = new MqttDeliveryTokenAndroid(
                  this, userContext, callback, message);
            String activityToken = storeToken(token);
            IMqttDeliveryToken internalToken =
        mqttService.publish(clientHandle,
                  topic, message, null, activityToken);
            token.setDelegate(internalToken);
            return token;
        }



        ------------------------------------------------------------------------
        *Da:* paho-dev-bounces@xxxxxxxxxxx
        <mailto:paho-dev-bounces@xxxxxxxxxxx>
        <paho-dev-bounces@xxxxxxxxxxx>
        <mailto:paho-dev-bounces@xxxxxxxxxxx> per conto di Sergio
        Torassa <sertorassa@xxxxxxxxxxx> <mailto:sertorassa@xxxxxxxxxxx>
        *Inviato:* mercoledì 17 maggio 2017 15.43
        *A:* paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
        *Oggetto:* Re: [paho-dev] NPE thrown by MqttAndroidClient
        without any boject set to null

        Does anyone know  what could be the reason of the NPE?


        I also made another try using the same identical code but
        replacing MqttAndroidClient with MqttClient and everything works
        fine.

        Debugging the code I found that the mqttService is null in
        MqttAndroidClient when the mqttService.publish(..) method is
        called.



        ------------------------------------------------------------------------
        *Da:* Sergio Torassa <sertorassa@xxxxxxxxxxx>
        <mailto:sertorassa@xxxxxxxxxxx>
        *Inviato:* sabato 6 maggio 2017 16.19
        *A:* paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
        *Oggetto:* Re: [paho-dev] NPE thrown by MqttAndroidClient
        without any boject set to null

        I tried creating a brand new project in AndroidStudio and the
        result is the same: NPE in the publish method


        In the gradle.build config the versions of MQTT are:


        serviceVersion = '1.1.1'
        clientVersion = '1.1.0'


        This is the excerpt for the connection, which works without
        catching any Exception


        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setCleanSession(false);

        try {

             mqttAndroidClient.connect(mqttConnectOptions, null);

        } catch (MqttException ex){
             ex.printStackTrace();
        }




        ------------------------------------------------------------------------
        *Da:* Sergio Torassa <sertorassa@xxxxxxxxxxx>
        <mailto:sertorassa@xxxxxxxxxxx>
        *Inviato:* martedì 2 maggio 2017 11.28
        *A:* paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
        *Oggetto:* Re: [paho-dev] NPE thrown by MqttAndroidClient
        without any boject set to null

        Hi James,


        this is the result in logcat of e.printStackTrace()


        java.lang.NullPointerException
              at
        org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:812)
              at
        org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:668)
              at
        com.storassa.android.giulia.MainActivity.onCreate(MainActivity.java:54)
              at android.app.Activity.performCreate(Activity.java:5275)
              at
        android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at
        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
              at
        android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
              at
        android.app.ActivityThread.access$800(ActivityThread.java:139)
              at
        android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5103)
              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:788)
              at
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
              at dalvik.system.NativeStart.main(Native Method)


        Just to add a piece of info, I connected the client with a
        simple call to MqttAndroidClient.connect().

        ------------------------------------------------------------------------
        *Da:* paho-dev-bounces@xxxxxxxxxxx
        <mailto:paho-dev-bounces@xxxxxxxxxxx>
        <paho-dev-bounces@xxxxxxxxxxx>
        <mailto:paho-dev-bounces@xxxxxxxxxxx> per conto di James Sutton1
        <james.sutton@xxxxxxxxxx> <mailto:james.sutton@xxxxxxxxxx>
        *Inviato:* martedì 2 maggio 2017 10.20
        *A:* paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
        *Oggetto:* Re: [paho-dev] NPE thrown by MqttAndroidClient
        without any boject set to null
        Hi Sergio,
        Could you provide the stack trace please, It might help with
        identifying the cause.
        Kind regards,
        *James Sutton*
        Software Engineer - IoT Foundation - MQTT Open Source Projects
        Ops Team - Wimbledon Project

        ------------------------------------------------------------------------
        Phone: 01962 815438 | Extension: x372454
        E-mail: james.sutton@xxxxxxxxxx <mailto:james.sutton@xxxxxxxxxx>
        Personal Website: www.jsutton.co.uk <http://www.jsutton.co.uk>
        Find me on: <http://www.jsutton.co.uk>
        <https://twitter.com/jpwsutton>
        	IBM

        Hursley Park
        Hursley, SO212JN
        United Kingdom

        IBM United Kingdom Limited Registered in England and Wales with
        number 741598 Registered office: PO Box 41, North Harbour,
        Portsmouth, Hants. PO6 3AU

            ----- Original message -----
            From: Sergio Torassa <sertorassa@xxxxxxxxxxx>
            <mailto:sertorassa@xxxxxxxxxxx>
            Sent by: paho-dev-bounces@xxxxxxxxxxx
            <mailto:paho-dev-bounces@xxxxxxxxxxx>
            To: General development discussions for paho project
            <paho-dev@xxxxxxxxxxx> <mailto:paho-dev@xxxxxxxxxxx>,
            "ingeniero@xxxxxxxxxxxxxxxxxxx"
            <mailto:ingeniero@xxxxxxxxxxxxxxxxxxx>
            <ingeniero@xxxxxxxxxxxxxxxxxxx>
            <mailto:ingeniero@xxxxxxxxxxxxxxxxxxx>
            Cc:
            Subject: Re: [paho-dev] NPE thrown by MqttAndroidClient
            without any boject set to null
            Date: Mon, May 1, 2017 7:40 PM

            I forgot to mention that also topic is not null. It is a string

            Il 01/mag/2017 20:01, Manuel Domínguez Dorado
            <manolodd@xxxxxxxxx> <mailto:manolodd@xxxxxxxxx> ha scritto:
             >
             > What about publishTopic?
             >
             > Perhaps this is null.
             >
             > 2017-05-01 19:26 GMT+02:00 Sergio Torassa
            <sertorassa@xxxxxxxxxxx> <mailto:sertorassa@xxxxxxxxxxx>:
             >>
             >> Hello all
             >>
             >>
             >> I'm trying to develop a very simple Android app based
            upon MQTT.
             >>
             >> Just from the beginning I started with an NPE thrown by
            MqttAndroidClient.publish(String topic, MqttMessage message)
            method.
             >>
             >>
             >> This is the excerpt of the code where the NPE is thrown:
             >>
             >>
             >> try {
             >>     MqttMessage msg = new MqttMessage("test".getBytes());
             >>     mqttAndroidClient.publish(publishTopic, msg);
             >> } catch (Exception e) {
             >>     throw new RuntimeException(e);
             >> }
             >>
             >> I checked both mqttAndroidClient and msg and neither of
            them are null.
             >>
             >> From the MQTT documentation MqttAndroidClient.publish()
            method is not supposed to throw a NPE.
             >>
             >> What could be the reason for that?
             >>
             >>
             >> Thanks
             >>
             >> Sergio
             >>
             >>
             >> _______________________________________________
             >> paho-dev mailing list
             >> paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
             >> To change your delivery options, retrieve your password,
            or unsubscribe from this list, visit
             >> https://dev.eclipse.org/mailman/listinfo/paho-dev
             >
             >
             >
             >
             > --
             > ---
             > Manuel Domínguez Dorado
             > Software engineer (Ph.D, M.Sc., B.Sc.)
             > Certified Project Management Professional (PMP)
             > ingeniero@xxxxxxxxxxxxxxxxxxx
            <mailto:ingeniero@xxxxxxxxxxxxxxxxxxx>
             > http://www.ManoloDominguez.com
             > (+34) 607 418 760
             >

            _______________________________________________
            paho-dev mailing list
            paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
            To change your delivery options, retrieve your password, or
            unsubscribe from this list, visit
            https://dev.eclipse.org/mailman/listinfo/paho-dev

        Unless stated otherwise above:
        IBM United Kingdom Limited - Registered in England and Wales
        with number 741598.
        Registered office: PO Box 41, North Harbour, Portsmouth,
        Hampshire PO6 3AU



        _______________________________________________
        paho-dev mailing list
        paho-dev@xxxxxxxxxxx <mailto:paho-dev@xxxxxxxxxxx>
        To change your delivery options, retrieve your password, or unsubscribe from this list, visit
        https://dev.eclipse.org/mailman/listinfo/paho-dev


-- Ian Craggs
    icraggs@xxxxxxxxxx <mailto:icraggs@xxxxxxxxxx>                  IBM United Kingdom
    Paho Project Lead; Committer on Mosquitto




_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev




Back to the top