From 42de0b3ae7c60f42b1bab847ce847715a0d5cb1c Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Thu, 29 Jan 2015 09:26:46 -0500 Subject: [PATCH] Fixed potential NullPointerExceptions Rather than try to correct the issue of the Comparator crashing in BookmarkManager because the Strings/HistoryItems were null, I modified the HistoryItem object so that the title, url, and folder strings can no longer be null but will instead be empty if set to null, this then prevents the BookmarkManager from throwing an NPE when sorting the items by title. --- src/acr/browser/lightning/BookmarkManager.java | 4 +--- src/acr/browser/lightning/HistoryItem.java | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/acr/browser/lightning/BookmarkManager.java b/src/acr/browser/lightning/BookmarkManager.java index d6de8f5..9904000 100644 --- a/src/acr/browser/lightning/BookmarkManager.java +++ b/src/acr/browser/lightning/BookmarkManager.java @@ -429,10 +429,8 @@ public class BookmarkManager { private class SortIgnoreCase implements Comparator { public int compare(HistoryItem o1, HistoryItem o2) { - if (o1 == null || o2 == null) { + if (o1 == null || o2 == null || o1.getTitle() == null || o2.getTitle() == null) { return 0; - } else if (o1.getTitle() == null || o2.getTitle() == null) { - return o1.getTitle().compareTo(o2.getTitle()); } return o1.getTitle().toLowerCase(Locale.getDefault()) .compareTo(o2.getTitle().toLowerCase(Locale.getDefault())); diff --git a/src/acr/browser/lightning/HistoryItem.java b/src/acr/browser/lightning/HistoryItem.java index eb891a9..2b5c770 100644 --- a/src/acr/browser/lightning/HistoryItem.java +++ b/src/acr/browser/lightning/HistoryItem.java @@ -67,7 +67,7 @@ public class HistoryItem implements Comparable { } public void setFolder(String folder) { - mFolder = folder; + mFolder = (folder == null) ? "" : folder; } public void setOrder(int order) { @@ -93,7 +93,7 @@ public class HistoryItem implements Comparable { // setting name public void setUrl(String url) { - this.mUrl = url; + this.mUrl = (url == null) ? "" : url; } // getting phone number @@ -103,7 +103,7 @@ public class HistoryItem implements Comparable { // setting phone number public void setTitle(String title) { - this.mTitle = title; + this.mTitle = (title == null) ? "" : title; } @Override