From 346aee49cb062b7e68986ab29fda1ae055a5da7c Mon Sep 17 00:00:00 2001 From: t2000 Date: Fri, 1 Jan 2021 21:45:55 +0100 Subject: [PATCH] [tradfri] Fix NPE if gateway is unreachable (#9629) Fixes #9628 Signed-off-by: Stefan Triller --- .../internal/handler/TradfriThingHandler.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/bundles/org.openhab.binding.tradfri/src/main/java/org/openhab/binding/tradfri/internal/handler/TradfriThingHandler.java b/bundles/org.openhab.binding.tradfri/src/main/java/org/openhab/binding/tradfri/internal/handler/TradfriThingHandler.java index 540f4bb8ddae2..8596fb53fa378 100644 --- a/bundles/org.openhab.binding.tradfri/src/main/java/org/openhab/binding/tradfri/internal/handler/TradfriThingHandler.java +++ b/bundles/org.openhab.binding.tradfri/src/main/java/org/openhab/binding/tradfri/internal/handler/TradfriThingHandler.java @@ -66,20 +66,21 @@ public synchronized void initialize() { this.id = getConfigAs(TradfriDeviceConfig.class).id; TradfriGatewayHandler handler = (TradfriGatewayHandler) tradfriGateway.getHandler(); - String uriString = handler.getGatewayURI() + "/" + id; - try { - URI uri = new URI(uriString); - coapClient = new TradfriCoapClient(uri); - coapClient.setEndpoint(handler.getEndpoint()); - } catch (URISyntaxException e) { - logger.debug("Illegal device URI `{}`: {}", uriString, e.getMessage()); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage()); - return; - } active = true; updateStatus(ThingStatus.UNKNOWN); switch (tradfriGateway.getStatus()) { case ONLINE: + String uriString = handler.getGatewayURI() + "/" + id; + try { + URI uri = new URI(uriString); + coapClient = new TradfriCoapClient(uri); + coapClient.setEndpoint(handler.getEndpoint()); + } catch (URISyntaxException e) { + logger.debug("Illegal device URI `{}`: {}", uriString, e.getMessage()); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage()); + return; + } + scheduler.schedule(() -> { observeRelation = coapClient.startObserve(this); }, 3, TimeUnit.SECONDS);