eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.12k stars 883 forks source link

Null Pointer Exception using Memory Persistence #522

Closed djbr-david closed 6 years ago

djbr-david commented 6 years ago

Please fill out the form below before submitting, thank you!

If this is a bug regarding the Android Service, please raise the bug here instead: https://github.com/eclipse/paho.mqtt.android/issues/new

Using Memory Persistence and am getting the above exception when calling the close method, but other methods could suffer the same issue. The setup code is:

            persistence = new MemoryPersistence();
            client = new MqttClient(host, clientId, persistence);

which generally works fine, but if the MqttClient fails to initialise, the persistence open method wont be called so later on when I try to call the close method as part of my disconnect handling it fails. Checking the File Persistence code, I see this situation is handled correctly, so for consistency Memory Persistence should exhibit the same behaviour which then means client code doesn't need to change if the underlying Persistence mechanism changes.

jpwsutton commented 6 years ago

Thanks for raising this issue @djbr-david, I've put a check in that will only attempt to close the Persistence layer if the hashtable has been initialised, I've also ported some improvements that are in the v5 client that throw an MqttPersistenceException when putting or getting if the persistence hasn't been initialised, so that should get rid of any null pointers would would have otherwise seen too.

djbr-david commented 6 years ago

Many thanks for your time and effort.