From 5e059ba0448e77adc03d40af1b6ff5cd069176a0 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 13 May 2023 21:25:23 +0200 Subject: [PATCH] Fix NullPointerException (#14989) Signed-off-by: Jacob Laursen --- .../discovery/HueBridgeNupnpDiscovery.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueBridgeNupnpDiscovery.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueBridgeNupnpDiscovery.java index 5113086b47943..0355926b1cb70 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueBridgeNupnpDiscovery.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/discovery/HueBridgeNupnpDiscovery.java @@ -17,10 +17,10 @@ import java.io.IOException; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.hue.internal.handler.HueBridgeHandler; import org.openhab.core.config.discovery.AbstractDiscoveryService; import org.openhab.core.config.discovery.DiscoveryResult; @@ -109,12 +109,12 @@ private boolean isReachableAndValidHueBridge(BridgeJsonParameters bridge) { return false; } if (id.length() < 10) { - logger.debug("Bridge not discovered: id {} is shorter then 10.", id); + logger.debug("Bridge not discovered: id {} is shorter than 10.", id); return false; } if (!BRIDGE_INDICATOR.equals(id.substring(6, 10))) { logger.debug( - "Bridge not discovered: id {} does not contain bridge indicator {} or its at the wrong position.", + "Bridge not discovered: id {} does not contain bridge indicator {} or it's at the wrong position.", id, BRIDGE_INDICATOR); return false; } @@ -124,8 +124,8 @@ private boolean isReachableAndValidHueBridge(BridgeJsonParameters bridge) { logger.debug("Bridge not discovered: Failure accessing description file for ip: {}", host); return false; } - if (!description.contains(MODEL_NAME_PHILIPS_HUE)) { - logger.debug("Bridge not discovered: Description does not containing the model name: {}", description); + if (description == null || !description.contains(MODEL_NAME_PHILIPS_HUE)) { + logger.debug("Bridge not discovered: Description does not contain the model name: {}", description); return false; } return true; @@ -140,14 +140,22 @@ private List getBridgeList() { try { Gson gson = new Gson(); String json = doGetRequest(DISCOVERY_URL); + if (json == null) { + logger.debug("Philips Hue NUPnP service call failed. Can't discover bridges"); + return List.of(); + } List bridgeParameters = gson.fromJson(json, new TypeToken>() { }.getType()); - return Objects.requireNonNull(bridgeParameters); + if (bridgeParameters == null) { + logger.debug("Philips Hue NUPnP service returned empty JSON. Can't discover bridges"); + return List.of(); + } + return bridgeParameters; } catch (IOException e) { logger.debug("Philips Hue NUPnP service not reachable. Can't discover bridges"); } catch (JsonParseException e) { - logger.debug("Invalid json respone from Hue NUPnP service. Can't discover bridges"); + logger.debug("Invalid json response from Hue NUPnP service. Can't discover bridges"); } return List.of(); } @@ -159,7 +167,7 @@ private List getBridgeList() { * @return the http request result as String * @throws IOException if request failed */ - protected String doGetRequest(String url) throws IOException { + protected @Nullable String doGetRequest(String url) throws IOException { return HttpUtil.executeUrl("GET", url, REQUEST_TIMEOUT); } }