Skip to content
Snippets Groups Projects
Commit 54daeaac authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

NPE no longer thrown when requesting data for a timestamp not in the list

NoSuchElementException is thrown instead.
Closes #1
parent a1690583
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ import java.io.IOException; ...@@ -12,6 +12,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
/** /**
...@@ -372,10 +373,10 @@ public class OpenWeatherConnector { ...@@ -372,10 +373,10 @@ public class OpenWeatherConnector {
* *
* @param listName the key to access the list * @param listName the key to access the list
* @param timestamp the timestamp of the list entry * @param timestamp the timestamp of the list entry
* @return the object of interest, or null if the list or the timestamp are not * @return the object of interest, or null if the list is not present
* 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; JSONObject listEntry = null;
if (data.containsKey(listName)) { if (data.containsKey(listName)) {
JSONArray list = (JSONArray) data.get(listName); JSONArray list = (JSONArray) data.get(listName);
...@@ -384,6 +385,9 @@ public class OpenWeatherConnector { ...@@ -384,6 +385,9 @@ public class OpenWeatherConnector {
listEntry = (JSONObject) entry; listEntry = (JSONObject) entry;
} }
} }
if (listEntry == null) {
throw new NoSuchElementException("The requested data is not available for " + timestamp + ".");
}
} }
return listEntry; return listEntry;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment