From 000d5329f73afd9b40844d2181f376c29e22171c Mon Sep 17 00:00:00 2001 From: lsiepel Date: Mon, 23 Sep 2024 22:43:59 +0200 Subject: [PATCH] [samsungtv] Fix `NullPointerException` (#17454) * Fix NPE Signed-off-by: Leo Siepel Signed-off-by: Ciprian Pascu --- .../service/SmartThingsApiService.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/service/SmartThingsApiService.java b/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/service/SmartThingsApiService.java index 4c61b74d687..09b721ccc02 100755 --- a/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/service/SmartThingsApiService.java +++ b/bundles/org.openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/service/SmartThingsApiService.java @@ -147,6 +147,10 @@ public class SmartThingsApiService implements SamsungTvService { class TvChannel { Values tvChannel; Values tvChannelName; + + public String getChannelNum() { + return Optional.ofNullable(tvChannel).map(a -> a.value).orElse(""); + } } class Values { @@ -246,8 +250,7 @@ public class SmartThingsApiService implements SamsungTvService { } public Number getTvChannel() { - return Optional.ofNullable(tvChannel).map(a -> a.tvChannel).map(a -> a.value).filter(i -> !i.isBlank()) - .map(j -> parseTVChannel(j)).orElse(-1f); + return Optional.ofNullable(tvChannel).map(a -> a.getChannelNum()).map(j -> parseTVChannel(j)).orElse(-1f); } public String getTvChannelName() { @@ -456,10 +459,9 @@ public class SmartThingsApiService implements SamsungTvService { public Number getTvChannel() { if (getCapabilityAttribute("tvChannel", "tvChannel")) { - return Optional.ofNullable(deviceEvent).map(a -> a.getValue()).filter(i -> !i.isBlank()) - .map(j -> parseTVChannel(j)).orElse(-1f); + return Optional.ofNullable(deviceEvent).map(a -> a.getValue()).map(j -> parseTVChannel(j)).orElse(-1f); } - return -1; + return -1f; } public String getTvChannelName() { @@ -470,15 +472,14 @@ public class SmartThingsApiService implements SamsungTvService { } } - public Number parseTVChannel(@Nullable String channel) { + public static Number parseTVChannel(@Nullable String channel) { try { - return channel != null - ? Float.parseFloat( - channel.replaceAll("\\D+", ".").replaceFirst("^\\D*((\\d+\\.\\d+)|(\\d+)).*", "$1")) - : -1f; + return (channel == null || channel.isBlank()) ? -1f + : Float.parseFloat( + channel.replaceAll("\\D+", ".").replaceFirst("^\\D*((\\d+\\.\\d+)|(\\d+)).*", "$1")); } catch (NumberFormatException ignore) { } - return -1; + return -1f; } public void updateTV() { @@ -790,10 +791,12 @@ public class SmartThingsApiService implements SamsungTvService { } Number tvChannel = d.getTvChannel(); if (tvChannel.intValue() != -1) { + logger.trace("{}: SSE Got TV Channel: {}", host, tvChannel); updateState(CHANNEL, tvChannel); - String tvChannelName = d.getTvChannelName(); - logger.trace("{}: SSE Got TV Channel Name: {} Channel: {}", host, tvChannelName, - tvChannel); + } + String tvChannelName = d.getTvChannelName(); + if (!tvChannelName.isBlank()) { + logger.trace("{}: SSE Got TV Channel Name: {}", host, tvChannelName); updateState(CHANNEL_NAME, tvChannelName); } String Power = d.getSwitch();