From a8a5e86fb9e3b87f3e6f7892e984adacf7afe83f Mon Sep 17 00:00:00 2001 From: J-N-K Date: Thu, 25 May 2023 19:19:06 +0200 Subject: [PATCH] Fix NPE in UpgradeTool when no pattern present (#3630) Reported on the forum. State descriptions do no necessarily contain a pattern and thus `.get("pattern")` can return null. Signed-off-by: Jan N. Klug --- .../openhab/core/tools/internal/Upgrader.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/tools/upgradetool/src/main/java/org/openhab/core/tools/internal/Upgrader.java b/tools/upgradetool/src/main/java/org/openhab/core/tools/internal/Upgrader.java index 789a0a7b4..5cf8daa78 100644 --- a/tools/upgradetool/src/main/java/org/openhab/core/tools/internal/Upgrader.java +++ b/tools/upgradetool/src/main/java/org/openhab/core/tools/internal/Upgrader.java @@ -94,27 +94,31 @@ public class Upgrader { ManagedItemProvider.PersistedItem item = itemStorage.get(itemName); if (item != null && item.itemType.startsWith("Number:")) { if (metadataStorage.containsKey(NumberItem.UNIT_METADATA_NAMESPACE + ":" + itemName)) { - logger.debug("{}: already contains a 'unit' metadata, skipping it", itemName); + logger.debug("{}: Already contains a 'unit' metadata, skipping it", itemName); } else { Metadata metadata = metadataStorage.get("stateDescription:" + itemName); if (metadata == null) { logger.debug("{}: Nothing to do, no state description found.", itemName); } else { String pattern = (String) metadata.getConfiguration().get("pattern"); - if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) { - logger.warn( - "{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!", - itemName); - } else { - Unit stateDescriptionUnit = UnitUtils.parseUnit(pattern); - if (stateDescriptionUnit != null) { - String unit = stateDescriptionUnit.toString(); - MetadataKey defaultUnitMetadataKey = new MetadataKey(NumberItem.UNIT_METADATA_NAMESPACE, + if (pattern != null) { + if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) { + logger.warn( + "{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!", itemName); - Metadata defaultUnitMetadata = new Metadata(defaultUnitMetadataKey, unit, null); - metadataStorage.put(defaultUnitMetadataKey.toString(), defaultUnitMetadata); - logger.info("{}: Wrote 'unit={}' to metadata.", itemName, unit); + } else { + Unit stateDescriptionUnit = UnitUtils.parseUnit(pattern); + if (stateDescriptionUnit != null) { + String unit = stateDescriptionUnit.toString(); + MetadataKey defaultUnitMetadataKey = new MetadataKey( + NumberItem.UNIT_METADATA_NAMESPACE, itemName); + Metadata defaultUnitMetadata = new Metadata(defaultUnitMetadataKey, unit, null); + metadataStorage.put(defaultUnitMetadataKey.toString(), defaultUnitMetadata); + logger.info("{}: Wrote 'unit={}' to metadata.", itemName, unit); + } } + } else { + logger.debug("{}: Nothing to do, no pattern found.", itemName); } } }