Fix NullPointerException (#14989)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2023-05-13 21:25:23 +02:00 committed by GitHub
parent 38df936aa9
commit 939c9caa1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,10 +17,10 @@ import static org.openhab.binding.hue.internal.HueBindingConstants.*;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.handler.HueBridgeHandler; import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
import org.openhab.core.config.discovery.AbstractDiscoveryService; import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult; import org.openhab.core.config.discovery.DiscoveryResult;
@ -109,12 +109,12 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
return false; return false;
} }
if (id.length() < 10) { 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; return false;
} }
if (!BRIDGE_INDICATOR.equals(id.substring(6, 10))) { if (!BRIDGE_INDICATOR.equals(id.substring(6, 10))) {
logger.debug( 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); id, BRIDGE_INDICATOR);
return false; return false;
} }
@ -124,8 +124,8 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
logger.debug("Bridge not discovered: Failure accessing description file for ip: {}", host); logger.debug("Bridge not discovered: Failure accessing description file for ip: {}", host);
return false; return false;
} }
if (!description.contains(MODEL_NAME_PHILIPS_HUE)) { if (description == null || !description.contains(MODEL_NAME_PHILIPS_HUE)) {
logger.debug("Bridge not discovered: Description does not containing the model name: {}", description); logger.debug("Bridge not discovered: Description does not contain the model name: {}", description);
return false; return false;
} }
return true; return true;
@ -140,14 +140,22 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
try { try {
Gson gson = new Gson(); Gson gson = new Gson();
String json = doGetRequest(DISCOVERY_URL); String json = doGetRequest(DISCOVERY_URL);
if (json == null) {
logger.debug("Philips Hue NUPnP service call failed. Can't discover bridges");
return List.of();
}
List<BridgeJsonParameters> bridgeParameters = gson.fromJson(json, List<BridgeJsonParameters> bridgeParameters = gson.fromJson(json,
new TypeToken<List<BridgeJsonParameters>>() { new TypeToken<List<BridgeJsonParameters>>() {
}.getType()); }.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) { } catch (IOException e) {
logger.debug("Philips Hue NUPnP service not reachable. Can't discover bridges"); logger.debug("Philips Hue NUPnP service not reachable. Can't discover bridges");
} catch (JsonParseException e) { } 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(); return List.of();
} }
@ -159,7 +167,7 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
* @return the http request result as String * @return the http request result as String
* @throws IOException if request failed * @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); return HttpUtil.executeUrl("GET", url, REQUEST_TIMEOUT);
} }
} }