From 40bd429619202f91085c2b48b8829c30505bd94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20K=C3=BCper?= Date: Mon, 16 Sep 2024 20:13:52 +0200 Subject: [PATCH] [homematic] Fix `NullPointerException` in discovery (#17390) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #17372: Fixed NPE Signed-off-by: Sönke Küper Signed-off-by: Ciprian Pascu --- .../HomematicDeviceDiscoveryService.java | 16 ++++++++++------ .../internal/handler/HomematicBridgeHandler.java | 6 +++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/discovery/HomematicDeviceDiscoveryService.java b/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/discovery/HomematicDeviceDiscoveryService.java index 91405efb9fb..e55d8db5803 100644 --- a/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/discovery/HomematicDeviceDiscoveryService.java +++ b/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/discovery/HomematicDeviceDiscoveryService.java @@ -78,7 +78,7 @@ public class HomematicDeviceDiscoveryService Thing bridge = thingHandler.getThing(); ThingStatus bridgeStatus = bridge.getStatus(); - if (ThingStatus.ONLINE == bridgeStatus) { + if (ThingStatus.ONLINE == bridgeStatus && gateway != null) { gateway.setInstallMode(true, getInstallModeDuration()); int remaining = gateway.getInstallMode(); @@ -109,7 +109,10 @@ public class HomematicDeviceDiscoveryService public synchronized void stopScan() { logger.debug("Stopping Homematic discovery scan"); disableInstallMode(); - thingHandler.getGateway().cancelLoadAllDeviceMetadata(); + final HomematicGateway gateway = thingHandler.getGateway(); + if (gateway != null) { + gateway.cancelLoadAllDeviceMetadata(); + } waitForScanFinishing(); super.stopScan(); } @@ -117,10 +120,11 @@ public class HomematicDeviceDiscoveryService private void disableInstallMode() { try { synchronized (installModeSync) { - if (isInInstallMode) { + final HomematicGateway gateway = thingHandler.getGateway(); + if (isInInstallMode && gateway != null) { isInInstallMode = false; installModeSync.notify(); - thingHandler.getGateway().setInstallMode(false, 0); + gateway.setInstallMode(false, 0); } } } catch (Exception ex) { @@ -172,10 +176,10 @@ public class HomematicDeviceDiscoveryService * Starts a thread which loads all Homematic devices connected to the gateway. */ public void loadDevices() { - if (loadDevicesFuture == null && thingHandler.getGateway() != null) { + final HomematicGateway gateway = thingHandler.getGateway(); + if (loadDevicesFuture == null && gateway != null) { loadDevicesFuture = scheduler.submit(() -> { try { - final HomematicGateway gateway = thingHandler.getGateway(); gateway.loadAllDeviceMetadata(); thingHandler.getTypeGenerator().validateFirmwares(); } catch (Throwable ex) { diff --git a/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/handler/HomematicBridgeHandler.java b/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/handler/HomematicBridgeHandler.java index 2d4bb424006..f8d42a64949 100644 --- a/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/handler/HomematicBridgeHandler.java +++ b/bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/handler/HomematicBridgeHandler.java @@ -24,6 +24,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; import org.openhab.binding.homematic.internal.common.HomematicConfig; import org.openhab.binding.homematic.internal.communicator.HomematicGateway; @@ -267,8 +268,11 @@ public class HomematicBridgeHandler extends BaseBridgeHandler implements Homemat } /** - * Returns the HomematicGateway. + * Returns the {@link HomematicGateway}. + * + * @return The gateway or null if gateway has not yet been initialized. */ + @Nullable public HomematicGateway getGateway() { return gateway; }