From b1753e589ecb15958e393d1d6a14303eb1bd4d41 Mon Sep 17 00:00:00 2001 From: crschnick Date: Fri, 1 Mar 2024 08:52:00 +0000 Subject: [PATCH] Fix prefs NPEs --- .../main/java/io/xpipe/app/prefs/AppPrefs.java | 18 ++++++++++++++++-- .../app/prefs/AppPrefsStorageHandler.java | 10 +++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java b/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java index 1dd59dd1b..83702a8c2 100644 --- a/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java +++ b/app/src/main/java/io/xpipe/app/prefs/AppPrefs.java @@ -428,6 +428,12 @@ public class AppPrefs { loadValue(globalStorageHandler, value); } + // How can this happen? + // Set to default if corrupted + if (storageDirectory.get() == null) { + storageDirectory.setValue(DEFAULT_STORAGE_DIR); + } + vaultStorageHandler = new AppPrefsStorageHandler(storageDirectory().getValue().resolve("preferences.json")); } @@ -458,10 +464,18 @@ public class AppPrefs { public void save() { for (Mapping m : mapping) { AppPrefsStorageHandler handler = m.isVaultSpecific() ? vaultStorageHandler : globalStorageHandler; + // It might be possible that we save while the vault handler is not initialized yet / has no file or directory + if (!handler.isInitialized()) { + continue; + } handler.updateObject(m.getKey(), m.getProperty().getValue()); } - vaultStorageHandler.save(); - globalStorageHandler.save(); + if (vaultStorageHandler.isInitialized()) { + vaultStorageHandler.save(); + } + if (globalStorageHandler.isInitialized()) { + globalStorageHandler.save(); + } } public void selectCategory(int selected) { diff --git a/app/src/main/java/io/xpipe/app/prefs/AppPrefsStorageHandler.java b/app/src/main/java/io/xpipe/app/prefs/AppPrefsStorageHandler.java index 903bc17a8..3453e9eaa 100644 --- a/app/src/main/java/io/xpipe/app/prefs/AppPrefsStorageHandler.java +++ b/app/src/main/java/io/xpipe/app/prefs/AppPrefsStorageHandler.java @@ -25,11 +25,19 @@ public class AppPrefsStorageHandler { this.file = file; } + boolean isInitialized() { + return content != null; + } + private JsonNode getContent(String key) { + loadIfNeeded(); + return content.get(key); + } + + private void loadIfNeeded() { if (content == null) { content = JsonConfigHelper.readConfigObject(file); } - return content.get(key); } private void setContent(String key, JsonNode value) {