From e5cb2ab38e77d7bc139be5ad25aa5413d1e598c2 Mon Sep 17 00:00:00 2001 From: Rahman Usta Date: Tue, 9 Jan 2024 00:23:53 +0100 Subject: [PATCH] Fix null pointer exception when closing tabs --- .../kodedu/controller/ApplicationController.java | 7 ++++++- .../engine/AsciidocAsciidoctorjConverter.java | 13 +++++++------ src/main/java/com/kodedu/other/Current.java | 6 +++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/kodedu/controller/ApplicationController.java b/src/main/java/com/kodedu/controller/ApplicationController.java index d9d0ca36e..7509f70fe 100644 --- a/src/main/java/com/kodedu/controller/ApplicationController.java +++ b/src/main/java/com/kodedu/controller/ApplicationController.java @@ -2397,7 +2397,12 @@ private void renderText() throws InterruptedException { String backend = (String) document.getAttribute("backend", "html5"); - ConverterResult converterResult = asciidoctorjConverter.convert(document, textChangeEvent); + EditorPane editorPane = current.currentEditor(); + if (Objects.isNull(editorPane)) { + return; + } + + ConverterResult converterResult = asciidoctorjConverter.convert(document, editorPane, textChangeEvent); this.lastConverterResult = converterResult; if (Objects.nonNull(latestTextChangeEvent.get())) { diff --git a/src/main/java/com/kodedu/engine/AsciidocAsciidoctorjConverter.java b/src/main/java/com/kodedu/engine/AsciidocAsciidoctorjConverter.java index d83323b0f..83b5a62e8 100644 --- a/src/main/java/com/kodedu/engine/AsciidocAsciidoctorjConverter.java +++ b/src/main/java/com/kodedu/engine/AsciidocAsciidoctorjConverter.java @@ -1,5 +1,6 @@ package com.kodedu.engine; +import com.kodedu.component.EditorPane; import com.kodedu.component.ViewPanel; import com.kodedu.config.*; import com.kodedu.controller.ApplicationController; @@ -97,18 +98,18 @@ public String applyReplacements(String asciidoc) { return null; } - public ConverterResult convert(Document document, TextChangeEvent textChangeEvent) { + public ConverterResult convert(Document document, EditorPane editorPane, TextChangeEvent textChangeEvent) { String backend = (String) document.getAttribute("backend", "html5"); Map attributes = document.getAttributes(); - current.currentEditor().updateAttributes(attributes); + editorPane.updateAttributes(attributes); return switch (backend) { - case "html5" -> convert(document, textChangeEvent, previewConfigBean); - case "revealjs" -> convert(document, textChangeEvent, revealjsConfigBean); + case "html5" -> convert(document, editorPane, textChangeEvent, previewConfigBean); + case "revealjs" -> convert(document, editorPane, textChangeEvent, revealjsConfigBean); default -> throw new RuntimeException("Backend not found: " + backend); }; } - private ConverterResult convert(Document document, TextChangeEvent textChangeEvent, AsciidoctorConfigBase configBean) { + private ConverterResult convert(Document document, EditorPane editorPane, TextChangeEvent textChangeEvent, AsciidoctorConfigBase configBean) { SafeMode safe = convertSafe(configBean.getSafe()); String backend = (String) document.getAttribute("backend", "html5"); Attributes attributes = configBean.getAsciiDocAttributes(document.getAttributes()); @@ -139,7 +140,7 @@ private ConverterResult convert(Document document, TextChangeEvent textChangeEve String content = textChangeEvent.getText(); String converted = asciidoctor.convert(content, options) ; Document finalDocument = (Document) DOCUMENT_MAP.get(docUUID); - current.currentEditor().setLastDocument(finalDocument); + editorPane.setLastDocument(finalDocument); DOCUMENT_MAP.remove(docUUID); logger.info("Converted Asciidoc to {}", backend.toUpperCase()); diff --git a/src/main/java/com/kodedu/other/Current.java b/src/main/java/com/kodedu/other/Current.java index e47e1da49..a7db98a3c 100644 --- a/src/main/java/com/kodedu/other/Current.java +++ b/src/main/java/com/kodedu/other/Current.java @@ -49,7 +49,11 @@ public WebView currentWebView() { } public EditorPane currentEditor() { - return currentTab().getEditorPane(); + MyTab currentTab = currentTab(); + if (Objects.isNull(currentTab)) { + return null; + } + return currentTab.getEditorPane(); } public WebEngine currentEngine() {