KM200 (KM50/100/200) Gateway: UNINITIALIZED - HANDLER_INITIALIZING_ERROR

After activation of binding within PaperUI, the following message appears:

Status: UNINITIALIZED - HANDLER_INITIALIZING_ERROR class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @3232ff)

Any hints?

Additional information from the startup log:

2020-11-03 18:42:58.783 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'ThingHandler.initialize()' on 'org.openhab.binding.km200.internal.handler.KM200GatewayHandler@1862238': class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @343e08)
java.lang.ClassCastException: class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @343e08)
	at org.openhab.binding.km200.internal.KM200Device.getServiceNode(KM200Device.java:364) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:59) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.determineServiceObject(KM200ServiceHandler.java:178) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:65) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.readCapabilities(KM200GatewayHandler.java:271) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.initialize(KM200GatewayHandler.java:121) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]
2020-11-03 18:42:58.853 [ERROR] [core.thing.internal.ThingManagerImpl] - Exception occurred while initializing handler of thing 'km200:kmdevice:207060676': class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @343e08)
java.lang.ClassCastException: class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @343e08)
	at org.openhab.binding.km200.internal.KM200Device.getServiceNode(KM200Device.java:364) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:59) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.determineServiceObject(KM200ServiceHandler.java:178) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:65) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.readCapabilities(KM200GatewayHandler.java:271) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.initialize(KM200GatewayHandler.java:121) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]

If this worked before it could be either the json returned by the device changed. Where it looks like it first contained an object and now it returns a primitive value. Or it returns data the binding didn’t expect. I don’t know know if the binding logs the json, but yo could try setting a log level debug or trace.

Even with log level TRACE there are no additional informations:

2020-11-03 19:06:26.258 [WARN ] [core.thing.internal.ThingManagerImpl] - Initializing handler for thing 'km200:kmdevice:207060676' takes more than 5000ms.
2020-11-03 19:06:28.234 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'ThingHandler.initialize()' on 'org.openhab.binding.km200.internal.handler.KM200GatewayHandler@1c7f8': class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @cf08db)
java.lang.ClassCastException: class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @cf08db)
	at org.openhab.binding.km200.internal.KM200Device.getServiceNode(KM200Device.java:364) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:59) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.determineServiceObject(KM200ServiceHandler.java:178) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:65) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.readCapabilities(KM200GatewayHandler.java:271) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.initialize(KM200GatewayHandler.java:121) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]
2020-11-03 19:06:28.313 [ERROR] [core.thing.internal.ThingManagerImpl] - Exception occurred while initializing handler of thing 'km200:kmdevice:207060676': class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @cf08db)
java.lang.ClassCastException: class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject (com.google.gson.JsonPrimitive and com.google.gson.JsonObject are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @cf08db)
	at org.openhab.binding.km200.internal.KM200Device.getServiceNode(KM200Device.java:364) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:59) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.determineServiceObject(KM200ServiceHandler.java:178) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200ServiceHandler.initObject(KM200ServiceHandler.java:65) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.readCapabilities(KM200GatewayHandler.java:271) ~[?:?]
	at org.openhab.binding.km200.internal.handler.KM200GatewayHandler.initialize(KM200GatewayHandler.java:121) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:834) [?:?]

That’s unfortunate. I did a quick look at the code. It looks like the data received is not what the code expects. You could try to modify the code and create a version of the binding and add a log to the code. Since its in initialization it could also the configuration of the thin is not correct and the device returns an error not expected by the binding. Otherwide create an issue on github to register this bug.

After enhancing the code with a lot of additional debugging, the issue could be focused onto the message sequence:

[b.binding.km200.internal.KM200Device] - /gateway/registrations: trying to query information.
[b.binding.km200.internal.KM200Device] - /gateway/registrations: trying to decode: [B@4de25f6.
[binding.km200.internal.KM200Cryption] - Length of message is 11.
[binding.km200.internal.KM200Cryption] - Did NOT decrypt message
[binding.km200.internal.KM200Cryption] - Exception on encoding

This confusing answer seems to be raised only on pretty old MB-LAN: I’ve found that the request for the attribute /gateway/registrations led to an invalid response of the gateway - blocking the binding to move forward into “bridge initialized” mode.

See PR.