diff --git a/src/main/java/org/opentripplanner/model/TimetableSnapshot.java b/src/main/java/org/opentripplanner/model/TimetableSnapshot.java index e6dcdd2eee1..08fe929a8f4 100644 --- a/src/main/java/org/opentripplanner/model/TimetableSnapshot.java +++ b/src/main/java/org/opentripplanner/model/TimetableSnapshot.java @@ -223,7 +223,7 @@ public boolean hasRealtimeAddedTripPatterns() { */ @Nullable public Route getRealtimeAddedRoute(FeedScopedId id) { - return realtimeAddedRoutes.get(id); + return getByNullableKey(id, realtimeAddedRoutes); } public Collection getAllRealTimeAddedRoutes() { @@ -235,7 +235,7 @@ public Collection getAllRealTimeAddedRoutes() { */ @Nullable public Trip getRealTimeAddedTrip(FeedScopedId id) { - return realTimeAddedTrips.get(id); + return getByNullableKey(id, realTimeAddedTrips); } public Collection getAllRealTimeAddedTrips() { @@ -247,7 +247,7 @@ public Collection getAllRealTimeAddedTrips() { */ @Nullable public TripPattern getRealTimeAddedPatternForTrip(Trip trip) { - return realTimeAddedPatternForTrip.get(trip); + return getByNullableKey(trip, realTimeAddedPatternForTrip); } /** @@ -262,7 +262,7 @@ public Collection getRealTimeAddedPatternForRoute(Route route) { */ @Nullable public TripOnServiceDate getRealTimeAddedTripOnServiceDateById(FeedScopedId id) { - return realTimeAddedTripOnServiceDateById.get(id); + return getByNullableKey(id, realTimeAddedTripOnServiceDateById); } /** @@ -272,7 +272,7 @@ public TripOnServiceDate getRealTimeAddedTripOnServiceDateById(FeedScopedId id) public TripOnServiceDate getRealTimeAddedTripOnServiceDateForTripAndDay( TripIdAndServiceDate tripIdAndServiceDate ) { - return realTimeAddedTripOnServiceDateForTripAndDay.get(tripIdAndServiceDate); + return getByNullableKey(tripIdAndServiceDate, realTimeAddedTripOnServiceDateForTripAndDay); } public Collection getAllRealTimeAddedTripOnServiceDate() { @@ -611,6 +611,20 @@ private Timetable copyTimetable(TripPattern pattern, LocalDate serviceDate, Time return tt; } + /** + * Look up the given key in a Map, return null if the key is null. + * This prevents a NullPointerException if the underlying implementation of the map does not + * accept querying with null keys (e.g. ImmutableMap). + * + **/ + @Nullable + private static V getByNullableKey(K key, Map map) { + if (key == null) { + return null; + } + return map.get(key); + } + protected static class SortedTimetableComparator implements Comparator { @Override