0.0.6 | NullPointerException Prevention

This commit is contained in:
Patrick
2026-06-01 20:40:38 +02:00
parent 3108e1fa58
commit 06ec1d8bda
2 changed files with 29 additions and 5 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ plugins {
} }
group = 'de.winniepat' group = 'de.winniepat'
version = '0.0.5' version = '0.0.6'
java { java {
@@ -118,13 +118,12 @@ public class LicenseClient {
JsonObject json = GSON.fromJson(body, JsonObject.class); JsonObject json = GSON.fromJson(body, JsonObject.class);
return new LicenseResult( return new LicenseResult(
json.get("valid").getAsBoolean(), getBoolean(json, "valid", false),
json.get("status").getAsString(), getString(json, "status", "unknown"),
json.get("message").getAsString() getString(json, "message", "")
); );
} }
/** /**
* Represents the result of a license check. * Represents the result of a license check.
* @param isValid boolean if the license is valid or not * @param isValid boolean if the license is valid or not
@@ -132,4 +131,29 @@ public class LicenseClient {
* @param message additional message from the backend * @param message additional message from the backend
*/ */
public record LicenseResult(boolean isValid, String status, String message) { } public record LicenseResult(boolean isValid, String status, String message) { }
/**
* Helper Method for getting Strings from the JsonObject without creating a NullPointerException
*
* @param json JsonObject to get the value from
* @param key key to get the value for
* @param def fallback value if Null
* @return the value from the JsonObject or the fallback value if the key is not present or null
*/
private static String getString(JsonObject json, String key, String def) {
if (!json.has(key) || json.get(key).isJsonNull()) return def;
return json.get(key).getAsString();
}
/**
* Helper Method for getting booleans from the JsonObject without creating a NullPointerException
* @param json JsonObject to get the value from
* @param key key to get the value for
* @param def fallback value if Null
* @return the value from the JsonObject or the fallback value if the key is not present or null
*/
private static boolean getBoolean(JsonObject json, String key, boolean def) {
if (!json.has(key) || json.get(key).isJsonNull()) return def;
return json.get(key).getAsBoolean();
}
} }