Fix prefs NPEs

This commit is contained in:
crschnick 2024-03-01 08:52:00 +00:00
parent 8591fe8562
commit b1753e589e
2 changed files with 25 additions and 3 deletions

View file

@ -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) {

View file

@ -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) {