From 93bd82930faeb54b9f877e23680620c7e51ecffb Mon Sep 17 00:00:00 2001 From: Alex <48492067+selectAll@users.noreply.github.com> Date: Mon, 19 Jun 2023 12:03:50 +0200 Subject: [PATCH] Fix: Check for null message before calling replaceAll to prevent NullPointerException. This could happen if a channel has no message. --- .../tools/rocketexporter/converter/SlackCsvFormat.java | 4 +++- .../tools/rocketexporter/converter/SlackCsvFormatTest.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/at/favre/tools/rocketexporter/converter/SlackCsvFormat.java b/src/main/java/at/favre/tools/rocketexporter/converter/SlackCsvFormat.java index 447cc03..c17341f 100644 --- a/src/main/java/at/favre/tools/rocketexporter/converter/SlackCsvFormat.java +++ b/src/main/java/at/favre/tools/rocketexporter/converter/SlackCsvFormat.java @@ -17,10 +17,12 @@ public void export(List messages, OutputStream outputStream) { writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); for (Message normalizedMessage : messages) { + String message = normalizedMessage.getMessage(); + message = message == null ? "" : message.replaceAll("\"", "\\\\\""); writer.write("\"" + normalizedMessage.getTimestamp().getEpochSecond() + "\"," + "\"" + normalizedMessage.getChannel() + "\"," + "\"" + normalizedMessage.getUsername() + "\"," + - "\"" + normalizedMessage.getMessage().replaceAll("\"", "\\\\\"") + "\"" + + "\"" + message + "\"" + "\n"); } writer.flush(); diff --git a/src/test/java/at/favre/tools/rocketexporter/converter/SlackCsvFormatTest.java b/src/test/java/at/favre/tools/rocketexporter/converter/SlackCsvFormatTest.java index 1bb7a7c..7fc3738 100644 --- a/src/test/java/at/favre/tools/rocketexporter/converter/SlackCsvFormatTest.java +++ b/src/test/java/at/favre/tools/rocketexporter/converter/SlackCsvFormatTest.java @@ -19,13 +19,15 @@ public void export() { exportFormat.export( List.of( new Message("m1", "u1", "c1", EPOCH), - new Message("m2", "u2", "c3", EPOCH.plusSeconds(1)) + new Message("m2", "u2", "c3", EPOCH.plusSeconds(1)), + new Message(null, "u3", "c3", EPOCH) ), bout); String out = bout.toString(); assertEquals("\"0\",\"c1\",\"u1\",\"m1\"\n" + - "\"1\",\"c3\",\"u2\",\"m2\"\n", out); + "\"1\",\"c3\",\"u2\",\"m2\"\n" + + "\"0\",\"c3\",\"u3\",\"\"\n", out); } }