From 2f055b2a8e62dec198dbe018e924c62af45e77aa Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 3 Aug 2024 09:03:19 -0700 Subject: [PATCH] Handle nulls if they happen --- .../bentobox/managers/island/IslandCache.java | 4 +++- .../managers/island/IslandCacheTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java index e6f91066d..41fc4fe64 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java @@ -538,7 +538,9 @@ public class IslandCache { * @return list of islands */ public @NonNull List getIslands(UUID uniqueId) { - return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById).toList(); + return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById) + .filter(Objects::nonNull) // Filter out null values + .toList(); } /** diff --git a/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java b/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java index f99961a4f..6c32505ae 100644 --- a/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java @@ -470,6 +470,23 @@ public class IslandCacheTest extends AbstractCommonSetup { assertTrue(ic.getIslands(owner).isEmpty()); } + /** + * Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#getIslands(java.util.UUID)}. + * @throws IntrospectionException + * @throws NoSuchMethodException + * @throws ClassNotFoundException + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws InstantiationException + */ + @Test + public void testGetIslandsUUIDNoIslands() throws InstantiationException, IllegalAccessException, + InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { + // Test is WIP. + when(handler.loadObject(anyString())).thenReturn(null); + assertTrue(ic.getIslands(owner).isEmpty()); + } + /** * Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#addIsland(world.bentobox.bentobox.database.objects.Island)}. */