From 54daeaac6d93f9a647decdf0381d81a3e09e0de0 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Sun, 12 Nov 2023 17:06:49 -0600 Subject: [PATCH] NPE no longer thrown when requesting data for a timestamp not in the list NoSuchElementException is thrown instead. Closes #1 --- .../connector/OpenWeatherConnector.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/edu/unl/cse/soft160/json_connections/connector/OpenWeatherConnector.java b/src/main/java/edu/unl/cse/soft160/json_connections/connector/OpenWeatherConnector.java index f0b3d47..7bedc47 100644 --- a/src/main/java/edu/unl/cse/soft160/json_connections/connector/OpenWeatherConnector.java +++ b/src/main/java/edu/unl/cse/soft160/json_connections/connector/OpenWeatherConnector.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.NoSuchElementException; import java.util.Set; /** @@ -372,10 +373,10 @@ public class OpenWeatherConnector { * * @param listName the key to access the list * @param timestamp the timestamp of the list entry - * @return the object of interest, or null if the list or the timestamp are not - * present + * @return the object of interest, or null if the list is not present + * @throws NoSuchElementException if the timestamp is not present in the list */ - private JSONObject getListEntry(String listName, Date timestamp) { + private JSONObject getListEntry(String listName, Date timestamp) throws NoSuchElementException { JSONObject listEntry = null; if (data.containsKey(listName)) { JSONArray list = (JSONArray) data.get(listName); @@ -384,6 +385,9 @@ public class OpenWeatherConnector { listEntry = (JSONObject) entry; } } + if (listEntry == null) { + throw new NoSuchElementException("The requested data is not available for " + timestamp + "."); + } } return listEntry; } -- GitLab