From 64f26f1df6829ff0fcdeb123d75e54e97fce1950 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 25 Oct 2019 15:08:40 +0300 Subject: [PATCH] Fixed NPE when player has no 'favorite server' Favorite server is determined by playtime and if player has no sessions the player's favorite server will be null, leading to NPE while parsing JSON. Added Optional to the source of the null value --- .../plan/delivery/domain/mutators/PerServerMutator.java | 4 ++-- .../plan/delivery/rendering/json/PlayerJSONParser.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PerServerMutator.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PerServerMutator.java index 116779737..0cccc314b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PerServerMutator.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/domain/mutators/PerServerMutator.java @@ -73,7 +73,7 @@ public class PerServerMutator { return timesMap; } - public UUID favoriteServer() { + public Optional favoriteServer() { long max = 0; UUID maxServer = null; @@ -85,7 +85,7 @@ public class PerServerMutator { } } - return maxServer; + return Optional.ofNullable(maxServer); } public Map> sessionsPerServer() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONParser.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONParser.java index f8e784843..a30e6b540 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONParser.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/PlayerJSONParser.java @@ -92,7 +92,7 @@ public class PlayerJSONParser { PlayerContainer player = db.query(new PlayerContainerQuery(playerUUID)); SessionsMutator sessionsMutator = SessionsMutator.forContainer(player); Map worldTimesPerServer = PerServerMutator.forContainer(player).worldTimesPerServer(); - List> serverAccordion = new ServerAccordion(player, serverNames, graphs, year, timeAmount, locale.get(GenericLang.UNKNOWN).toString()).asMaps(); + List> serverAccordion = new ServerAccordion(player, serverNames, graphs, year, timeAmount, locale.getString(GenericLang.UNKNOWN)).asMaps(); List kills = player.getValue(PlayerKeys.PLAYER_KILLS).orElse(Collections.emptyList()); List deaths = player.getValue(PlayerKeys.PLAYER_DEATHS_KILLS).orElse(Collections.emptyList()); @@ -176,8 +176,7 @@ public class PlayerJSONParser { info.put("session_median", timeAmount.apply(sessions.toMedianSessionLength())); info.put("activity_index", decimals.apply(activityIndex.getValue())); info.put("activity_index_group", activityIndex.getGroup()); - UUID favoriteServer = perServer.favoriteServer(); - info.put("favorite_server", serverNames.getOrDefault(favoriteServer, favoriteServer.toString())); + info.put("favorite_server", perServer.favoriteServer().map(favoriteServer -> serverNames.getOrDefault(favoriteServer, favoriteServer.toString())).orElse(locale.getString(GenericLang.UNKNOWN))); double averagePing = ping.average(); int worstPing = ping.max(); int bestPing = ping.min();