From 6833711ebedbca522d3682d10ee1bd8b9ecb0d50 Mon Sep 17 00:00:00 2001 From: eugen Date: Wed, 13 Jul 2022 17:01:02 +0200 Subject: [PATCH] [homekit] fix potential null pointer exceptions (#13122) * fix potential null pointer exceptions * use contains key Signed-off-by: Eugen Freiter --- .../org/openhab/io/homekit/internal/HomekitImpl.java | 2 ++ .../internal/accessories/HomekitAccessoryFactory.java | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitImpl.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitImpl.java index 7fa41890faeb2..563c6a3f486a7 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitImpl.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitImpl.java @@ -148,6 +148,8 @@ protected synchronized void modified(Map config) { try { HomekitSettings oldSettings = settings; settings = processConfig(config); + if ((oldSettings == null) || (settings == null)) + return; changeListener.updateSettings(settings); if (!oldSettings.networkInterface.equals(settings.networkInterface) || oldSettings.port != settings.port || oldSettings.useOHmDNS != settings.useOHmDNS) { diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java index 7cd25e2ee00f3..a9d1b0d350728 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java @@ -142,16 +142,17 @@ public class HomekitAccessoryFactory { }; private static List getRequiredCharacteristics(HomekitTaggedItem taggedItem) { + final List characteristics = new ArrayList<>(); + if (MANDATORY_CHARACTERISTICS.containsKey(taggedItem.getAccessoryType())) { + characteristics.addAll(Arrays.asList(MANDATORY_CHARACTERISTICS.get(taggedItem.getAccessoryType()))); + } if (taggedItem.getAccessoryType() == BATTERY) { final String isChargeable = taggedItem.getConfiguration(HomekitBatteryImpl.BATTERY_TYPE, "false"); if ("true".equalsIgnoreCase(isChargeable) || "yes".equalsIgnoreCase(isChargeable)) { - final List characteristics = new ArrayList<>(); - characteristics.addAll(Arrays.asList(MANDATORY_CHARACTERISTICS.get(taggedItem.getAccessoryType()))); characteristics.add(BATTERY_CHARGING_STATE); - return characteristics; } } - return Arrays.asList(MANDATORY_CHARACTERISTICS.get(taggedItem.getAccessoryType())); + return characteristics; } /**