Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Calling isConnected in MqttAndroidClient soon after initialization causes NPE

Hello everyone!

I am calling

client = new MqttAndroidClient(context, "tcp://192.168.123.177:1883", "XXXX");

and then soon after I call my method connectToMqttServer() which checks if (!client.isConnected()) before attempting to connect.

Going through the source code I see that isConnected is implemented as follows.

public boolean isConnected() {
    mqttService.isConnected(clientHandle);
}


However because I haven't called connect, mqttService will be null and it will thrown a NullPointerException.

I changed it as follows.

return mqttService != null && mqttService.isConnected(clientHandle);

Are there any potential pitfalls in doing this ? Is this the correct way ?

I personally think that it's natural to call isConnected() before calling connect()

Any ideas/comments would be appreciated.

Thanks!

Ranhiru


Back to the top