Skip to content

Commit bb03909

Browse files
committed
Catch NPE from getStringValueOfAJsonKey
1 parent ce5cc1f commit bb03909

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

  • src/main/java/io/github/hikingc/matrixsdk/services/utils

‎src/main/java/io/github/hikingc/matrixsdk/services/utils/Mapper.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ private static ObjectMapper buildMapper() {
4444
public static String getStringValueOfAJsonKey(String json, String key) {
4545
JsonNode tree = INSTANCE.readTree(json);
4646
if (tree == null || tree.isMissingNode()) {
47+
throw new MatrixIOException("Empty or malformed server response");
48+
}
49+
JsonNode value = tree.get(key);
50+
if (value == null) {
4751
throw new MatrixIOException("Missing '%s' in server response".formatted(key));
4852
}
49-
50-
return tree.get(key).stringValue();
53+
return value.asString();
5154
}
5255

5356
/// Attempts to extract a key value from a deserialized JSON Object to a [List]. Useful for
@@ -58,7 +61,8 @@ public static String getStringValueOfAJsonKey(String json, String key) {
5861
/// @param elementType the [Class] to deserialize each element into.
5962
/// @param <T> the type to deserialize each element into
6063
/// @return the deserialized [List] of values for the given key
61-
/// @throws MatrixIOException when the key was not in the response or the key value was not an Array
64+
/// @throws MatrixIOException when the key was not in the response or the key value was not an
65+
/// Array
6266
public static <T> List<T> getListFromAJsonKey(String json, String key, Class<T> elementType) {
6367
JsonNode tree = INSTANCE.readTree(json);
6468
JsonNode value = tree.get(key);

0 commit comments

Comments
 (0)