From 105b5244b894f95aead56e34e2ef3a8abf75f1f0 Mon Sep 17 00:00:00 2001 From: lo92fr Date: Sun, 15 Mar 2026 19:56:53 +0100 Subject: [PATCH] Fixes for null pointer error when compiling into eclipse (#5414) Signed-off-by: Laurent ARNAL --- .../openhab/core/config/core/ConfigUtil.java | 4 ++++ .../internal/validation/MinMaxValidator.java | 20 ++++++++----------- .../item/internal/GenericItemProvider.java | 5 ++++- .../DefaultSystemChannelTypeProvider.java | 10 ++++++++++ .../core/thing/internal/ThingImpl.java | 6 ++---- .../ChannelStateDescriptionProviderTest.java | 7 +++++++ 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java index 430152b0b..cf1d0ad45 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java @@ -263,6 +263,10 @@ public class ConfigUtil { final List lst = new ArrayList<>(collection.size()); for (final Object it : collection) { final Object normalized = normalizeType(it, null); + if (normalized == null) { + continue; + } + lst.add(normalized); if (normalized.getClass() != lst.getFirst().getClass()) { throw new IllegalArgumentException( diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/validation/MinMaxValidator.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/validation/MinMaxValidator.java index fb146147d..e6dc1a096 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/validation/MinMaxValidator.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/validation/MinMaxValidator.java @@ -49,19 +49,15 @@ final class MinMaxValidator implements ConfigDescriptionParameterValidator { } TypeIntrospection typeIntrospection = TypeIntrospections.get(parameter.getType()); - if (parameter.getMinimum() != null) { - BigDecimal min = parameter.getMinimum(); - if (typeIntrospection.isMinViolated(value, min)) { - return createMinMaxViolationMessage(parameter.getName(), typeIntrospection.getMinViolationMessageKey(), - min); - } + BigDecimal min = parameter.getMinimum(); + if (min != null && typeIntrospection.isMinViolated(value, min)) { + return createMinMaxViolationMessage(parameter.getName(), typeIntrospection.getMinViolationMessageKey(), + min); } - if (parameter.getMaximum() != null) { - BigDecimal max = parameter.getMaximum(); - if (typeIntrospection.isMaxViolated(value, max)) { - return createMinMaxViolationMessage(parameter.getName(), typeIntrospection.getMaxViolationMessageKey(), - max); - } + BigDecimal max = parameter.getMaximum(); + if (max != null && typeIntrospection.isMaxViolated(value, max)) { + return createMinMaxViolationMessage(parameter.getName(), typeIntrospection.getMaxViolationMessageKey(), + max); } return null; } diff --git a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java index c9cc81769..b7fb77807 100644 --- a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java +++ b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java @@ -556,7 +556,10 @@ public class GenericItemProvider extends AbstractProvider }; Item baseItem = createItemOfType(baseItemType, modelItem.getName()); - return applyGroupFunction(baseItem, modelItem, function); + if (baseItem != null) { + return applyGroupFunction(baseItem, modelItem, function); + } + return null; } /** diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/DefaultSystemChannelTypeProvider.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/DefaultSystemChannelTypeProvider.java index bd8fb26a5..537570a69 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/DefaultSystemChannelTypeProvider.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/DefaultSystemChannelTypeProvider.java @@ -437,9 +437,14 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider { final List allChannelTypes = new ArrayList<>(); final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class); + if (bundle == null) { + throw new IllegalArgumentException("bundle==null in DefaultSystemChannelTypeProvider::getChannelTypes"); + } + for (final ChannelType channelType : CHANNEL_TYPES) { allChannelTypes.add(createLocalizedChannelType(bundle, channelType, locale)); } + return allChannelTypes; } @@ -447,11 +452,16 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider { public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class); + if (bundle == null) { + throw new IllegalArgumentException("bundle==null in DefaultSystemChannelTypeProvider::getChannelType"); + } + for (final ChannelType channelType : CHANNEL_TYPES) { if (channelTypeUID.equals(channelType.getUID())) { return createLocalizedChannelType(bundle, channelType, locale); } } + return null; } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingImpl.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingImpl.java index 00116b131..74ebd4209 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingImpl.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ThingImpl.java @@ -214,10 +214,8 @@ public class ThingImpl implements Thing { throw new IllegalArgumentException("Property name must not be null or empty"); } synchronized (this) { - if (value == null) { - return properties.remove(name); - } - return properties.put(name, value); + String val = value; + return val == null ? properties.remove(name) : properties.put(name, val); } } diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/ChannelStateDescriptionProviderTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/ChannelStateDescriptionProviderTest.java index 7cfa499ef..ff0b0a4b9 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/ChannelStateDescriptionProviderTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/ChannelStateDescriptionProviderTest.java @@ -81,6 +81,7 @@ public class ChannelStateDescriptionProviderTest { ChannelType channelType1 = Mockito.mock(ChannelType.class); + assertNotNull(stateDescription1); when(channelType1.getState()).thenReturn(stateDescription1); when(thingTypeRegistry.getChannelType(channel1, Locale.ENGLISH)).thenReturn(channelType1); when(dynamicStateDescriptionProvider.getStateDescription(channel1, stateDescription1, Locale.ENGLISH)) @@ -93,6 +94,8 @@ public class ChannelStateDescriptionProviderTest { .withMaximum(new BigDecimal(100)).withStep(BigDecimal.ONE).withReadOnly(channel2State).withPattern("%s") .build().toStateDescription(); ChannelType channelType2 = Mockito.mock(ChannelType.class); + assertNotNull(stateDescription2); + when(channelType2.getState()).thenReturn(stateDescription2); when(thingTypeRegistry.getChannelType(channel2, Locale.ENGLISH)).thenReturn(channelType2); when(dynamicStateDescriptionProvider.getStateDescription(channel2, stateDescription2, Locale.ENGLISH)) @@ -117,6 +120,8 @@ public class ChannelStateDescriptionProviderTest { .withMaximum(new BigDecimal(100)).withStep(BigDecimal.ONE).withReadOnly(Boolean.TRUE).withPattern("%s") .build().toStateDescription(); ChannelType channelType = Mockito.mock(ChannelType.class); + + assertNotNull(stateDescription1); when(channelType.getState()).thenReturn(stateDescription1); when(thingTypeRegistry.getChannelType(channel1, Locale.ENGLISH)).thenReturn(channelType); @@ -142,6 +147,8 @@ public class ChannelStateDescriptionProviderTest { .withMaximum(new BigDecimal(100)).withStep(BigDecimal.ONE).withReadOnly(Boolean.FALSE).withPattern("%s") .build().toStateDescription(); ChannelType channelType = Mockito.mock(ChannelType.class); + + assertNotNull(stateDescription1); when(channelType.getState()).thenReturn(stateDescription1); when(thingTypeRegistry.getChannelType(channel1, Locale.ENGLISH)).thenReturn(channelType);