From 5c6fabbb62846e0c1a932856f1db2d9c021eaaac Mon Sep 17 00:00:00 2001 From: lsiepel Date: Fri, 18 Aug 2023 16:33:39 +0200 Subject: [PATCH] [denonmarantz] Fix NPE on connection problem (#15422) * Checkstyle cleanup * Fix #15444 Signed-off-by: lsiepel --- .../DenonMarantzStateChangedListener.java | 4 +-- .../DenonMarantzTelnetClientThread.java | 9 +++-- .../telnet/DenonMarantzTelnetConnector.java | 36 +++++++++---------- .../telnet/DenonMarantzTelnetListener.java | 5 ++- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/DenonMarantzStateChangedListener.java b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/DenonMarantzStateChangedListener.java index 72ac7ce6a0cdd..1feddeefac6c8 100644 --- a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/DenonMarantzStateChangedListener.java +++ b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/DenonMarantzStateChangedListener.java @@ -12,11 +12,11 @@ */ package org.openhab.binding.denonmarantz.internal; -import org.openhab.binding.denonmarantz.internal.handler.DenonMarantzHandler; import org.openhab.core.types.State; /** - * Interface to notify the {@link DenonMarantzHandler} about state changes. + * Interface to notify the {@link org.openhab.binding.denonmarantz.internal.handler.DenonMarantzHandler} about state + * changes. * * @author Jan-Willem Veldhuis - Initial contribution * diff --git a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetClientThread.java b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetClientThread.java index 0b4747271759d..a2fd6f3b7bc93 100644 --- a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetClientThread.java +++ b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetClientThread.java @@ -20,6 +20,7 @@ import java.net.Socket; import java.net.SocketTimeoutException; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.denonmarantz.internal.config.DenonMarantzConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,7 +49,7 @@ public class DenonMarantzTelnetClientThread extends Thread { private OutputStreamWriter out; - private BufferedReader in; + private @Nullable BufferedReader in; public DenonMarantzTelnetClientThread(DenonMarantzConfiguration config, DenonMarantzTelnetListener listener) { logger.debug("Denon listener created"); @@ -65,7 +66,11 @@ public void run() { do { try { - String line = in.readLine(); + String line = null; + BufferedReader in = this.in; + if (in != null) { + line = in.readLine(); + } if (line == null) { logger.debug("No more data read from client. Disconnecting.."); listener.telnetClientConnected(false); diff --git a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetConnector.java b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetConnector.java index d5b517407280f..2e9c73a838522 100644 --- a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetConnector.java +++ b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetConnector.java @@ -157,8 +157,8 @@ public void receivedLine(String line) { state.setInput(value); break; case "PW": // Power - if (value.equals("ON") || value.equals("STANDBY")) { - state.setPower(value.equals("ON")); + if ("ON".equals(value) || "STANDBY".equals(value)) { + state.setPower("ON".equals(value)); } break; case "MS": // Main zone surround program @@ -170,18 +170,18 @@ public void receivedLine(String line) { } break; case "MU": // Main zone mute - if (value.equals("ON") || value.equals("OFF")) { - state.setMute(value.equals("ON")); + if ("ON".equals(value) || "OFF".equals(value)) { + state.setMute("ON".equals(value)); } break; case "NS": // Now playing information processTitleCommand(value); break; case "Z2": // Zone 2 - if (value.equals("ON") || value.equals("OFF")) { - state.setZone2Power(value.equals("ON")); - } else if (value.equals("MUON") || value.equals("MUOFF")) { - state.setZone2Mute(value.equals("MUON")); + if ("ON".equals(value) || "OFF".equals(value)) { + state.setZone2Power("ON".equals(value)); + } else if ("MUON".equals(value) || "MUOFF".equals(value)) { + state.setZone2Mute("MUON".equals(value)); } else if (value.chars().allMatch(Character::isDigit)) { state.setZone2Volume(fromDenonValue(value)); } else { @@ -189,10 +189,10 @@ public void receivedLine(String line) { } break; case "Z3": // Zone 3 - if (value.equals("ON") || value.equals("OFF")) { - state.setZone3Power(value.equals("ON")); - } else if (value.equals("MUON") || value.equals("MUOFF")) { - state.setZone3Mute(value.equals("MUON")); + if ("ON".equals(value) || "OFF".equals(value)) { + state.setZone3Power("ON".equals(value)); + } else if ("MUON".equals(value) || "MUOFF".equals(value)) { + state.setZone3Mute("MUON".equals(value)); } else if (value.chars().allMatch(Character::isDigit)) { state.setZone3Volume(fromDenonValue(value)); } else { @@ -200,10 +200,10 @@ public void receivedLine(String line) { } break; case "Z4": // Zone 4 - if (value.equals("ON") || value.equals("OFF")) { - state.setZone4Power(value.equals("ON")); - } else if (value.equals("MUON") || value.equals("MUOFF")) { - state.setZone4Mute(value.equals("MUON")); + if ("ON".equals(value) || "OFF".equals(value)) { + state.setZone4Power("ON".equals(value)); + } else if ("MUON".equals(value) || "MUOFF".equals(value)) { + state.setZone4Mute("MUON".equals(value)); } else if (value.chars().allMatch(Character::isDigit)) { state.setZone4Volume(fromDenonValue(value)); } else { @@ -211,8 +211,8 @@ public void receivedLine(String line) { } break; case "ZM": // Main zone - if (value.equals("ON") || value.equals("OFF")) { - state.setMainZonePower(value.equals("ON")); + if ("ON".equals(value) || "OFF".equals(value)) { + state.setMainZonePower("ON".equals(value)); } break; } diff --git a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetListener.java b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetListener.java index 89b8ef9b62448..cade140540477 100644 --- a/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetListener.java +++ b/bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/telnet/DenonMarantzTelnetListener.java @@ -12,10 +12,9 @@ */ package org.openhab.binding.denonmarantz.internal.connector.telnet; -import org.openhab.binding.denonmarantz.internal.connector.DenonMarantzConnector; - /** - * Listener interface used to notify the {@link DenonMarantzConnector} about received messages over Telnet + * Listener interface used to notify the + * {@link org.openhab.binding.denonmarantz.internal.connector.DenonMarantzConnector} about received messages over Telnet * * @author Jan-Willem Veldhuis - Initial contribution *