Skip to content

Commit

Permalink
Fixed potential NPE on error reading json
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiwinter committed Jun 23, 2018
1 parent 098141c commit 789274c
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -70,7 +71,11 @@ public static Set<UserNotification> getNotificationsFromJson(URL url) throws IOE

Type listType = new TypeToken<Set<UserNotification>>() {
}.getType();
return createGson().fromJson(reader, listType);
Set<UserNotification> notifications = createGson().fromJson(reader, listType);
if (notifications == null) {
notifications = new HashSet<>();
}
return notifications;
} catch (JsonSyntaxException | JsonIOException e) {
Log.e(RemoteNotifications.TAG, e.getMessage() + ": " + url);
return Collections.emptySet();
Expand All @@ -90,7 +95,11 @@ public static Set<UserNotification> getNotificationsFromJson(URL url) throws IOE
public static Set<PersistentNotification> getPersistentNotificationsFromJson(String json) {
Type listType = new TypeToken<Set<PersistentNotification>>() {
}.getType();
return createGson().fromJson(json, listType);
Set<PersistentNotification> notifications = createGson().fromJson(json, listType);
if (notifications == null) {
notifications = new HashSet<>();
}
return notifications;
}

private static Gson createGson() {
Expand Down

0 comments on commit 789274c

Please sign in to comment.