Wrap all cursor for null pointer check

This commit is contained in:
Victor Shcherb 2018-05-23 00:09:17 +02:00
parent 0bf6829029
commit 8169dd91a8
7 changed files with 114 additions and 73 deletions

View file

@ -732,7 +732,7 @@ public class FavouritesDbHelper {
"SELECT " + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " + FAVOURITE_COL_LAT + "," + FAVOURITE_COL_LON + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ "SELECT " + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " + FAVOURITE_COL_LAT + "," + FAVOURITE_COL_LON + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
FAVOURITE_TABLE_NAME, null); FAVOURITE_TABLE_NAME, null);
cachedFavoritePoints.clear(); cachedFavoritePoints.clear();
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
String name = query.getString(0); String name = query.getString(0);
String cat = query.getString(1); String cat = query.getString(1);
@ -748,7 +748,9 @@ public class FavouritesDbHelper {
} }
} while (query.moveToNext()); } while (query.moveToNext());
} }
if (query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }

View file

@ -523,12 +523,14 @@ public class GPXDatabase {
if (db != null) { if (db != null) {
try { try {
SQLiteCursor query = db.rawQuery(GPX_TABLE_SELECT, null); SQLiteCursor query = db.rawQuery(GPX_TABLE_SELECT, null);
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
items.add(readItem(query)); items.add(readItem(query));
} while (query.moveToNext()); } while (query.moveToNext());
} }
if (query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }
@ -546,10 +548,12 @@ public class GPXDatabase {
String fileDir = getFileDir(file); String fileDir = getFileDir(file);
SQLiteCursor query = db.rawQuery(GPX_TABLE_SELECT + " WHERE " + GPX_COL_NAME + " = ? AND " + SQLiteCursor query = db.rawQuery(GPX_TABLE_SELECT + " WHERE " + GPX_COL_NAME + " = ? AND " +
GPX_COL_DIR + " = ?", new String[] { fileName, fileDir }); GPX_COL_DIR + " = ?", new String[] { fileName, fileDir });
if (query.moveToFirst()) { if ( query != null && query.moveToFirst()) {
result = readItem(query); result = readItem(query);
} }
if (query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }

View file

@ -374,7 +374,7 @@ public class SearchHistoryHelper {
// LEGACY QUERY !! // LEGACY QUERY !!
SQLiteCursor query = db.rawQuery( SQLiteCursor query = db.rawQuery(
"SELECT name, latitude, longitude, time FROM history ORDER BY time DESC", null); //$NON-NLS-1$//$NON-NLS-2$ "SELECT name, latitude, longitude, time FROM history ORDER BY time DESC", null); //$NON-NLS-1$//$NON-NLS-2$
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
String name = query.getString(0); String name = query.getString(0);
String type = PointDescription.POINT_TYPE_MARKER; String type = PointDescription.POINT_TYPE_MARKER;
@ -401,8 +401,10 @@ public class SearchHistoryHelper {
entries.add(e); entries.add(e);
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) {
query.close(); query.close();
} }
}
return entries; return entries;
} }
@ -416,7 +418,7 @@ public class SearchHistoryHelper {
HISTORY_COL_TIME + ", " + HISTORY_COL_FREQ_INTERVALS + ", " + HISTORY_COL_FREQ_VALUES + HISTORY_COL_TIME + ", " + HISTORY_COL_FREQ_INTERVALS + ", " + HISTORY_COL_FREQ_VALUES +
" FROM " + HISTORY_TABLE_NAME , null); //$NON-NLS-1$//$NON-NLS-2$ " FROM " + HISTORY_TABLE_NAME , null); //$NON-NLS-1$//$NON-NLS-2$
Map<PointDescription, HistoryEntry> st = new HashMap<PointDescription, HistoryEntry>(); Map<PointDescription, HistoryEntry> st = new HashMap<PointDescription, HistoryEntry>();
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
boolean reinsert = false; boolean reinsert = false;
do { do {
String name = query.getString(0); String name = query.getString(0);
@ -443,7 +445,9 @@ public class SearchHistoryHelper {
} }
} }
if(query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }

View file

@ -213,13 +213,15 @@ public class MapMarkersDbHelper {
if (db != null) { if (db != null) {
try { try {
SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT, null); SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT, null);
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
MapMarkersGroup group = readGroup(query); MapMarkersGroup group = readGroup(query);
res.put(group.getId(), group); res.put(group.getId(), group);
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }
@ -392,10 +394,12 @@ public class MapMarkersDbHelper {
if (db != null) { if (db != null) {
try { try {
SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ID + " = ?", new String[]{id}); SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ID + " = ?", new String[]{id});
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
res = readItem(query); res = readItem(query);
} }
if(query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }
@ -411,14 +415,16 @@ public class MapMarkersDbHelper {
try { try {
SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ACTIVE + " = ?", SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ACTIVE + " = ?",
new String[]{String.valueOf(1)}); new String[]{String.valueOf(1)});
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
MapMarker marker = readItem(query); MapMarker marker = readItem(query);
markers.put(marker.id, marker); markers.put(marker.id, marker);
nextKeys.add(marker.nextKey); nextKeys.add(marker.nextKey);
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }
@ -570,12 +576,14 @@ public class MapMarkersDbHelper {
try { try {
SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ACTIVE + " = ?", SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ACTIVE + " = ?",
new String[]{String.valueOf(0)}); new String[]{String.valueOf(0)});
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
markers.add(readItem(query)); markers.add(readItem(query));
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) {
query.close(); query.close();
}
} finally { } finally {
db.close(); db.close();
} }

View file

@ -473,7 +473,7 @@ public class PoiFiltersHelper {
SQLiteCursor query = conn.rawQuery("SELECT " + CATEGORIES_FILTER_ID + ", " + CATEGORIES_COL_CATEGORY + "," + CATEGORIES_COL_SUBCATEGORY + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ SQLiteCursor query = conn.rawQuery("SELECT " + CATEGORIES_FILTER_ID + ", " + CATEGORIES_COL_CATEGORY + "," + CATEGORIES_COL_SUBCATEGORY + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
CATEGORIES_NAME, null); CATEGORIES_NAME, null);
Map<String, Map<PoiCategory, LinkedHashSet<String>>> map = new LinkedHashMap<String, Map<PoiCategory, LinkedHashSet<String>>>(); Map<String, Map<PoiCategory, LinkedHashSet<String>>> map = new LinkedHashMap<String, Map<PoiCategory, LinkedHashSet<String>>>();
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
String filterId = query.getString(0); String filterId = query.getString(0);
if (!map.containsKey(filterId)) { if (!map.containsKey(filterId)) {
@ -492,11 +492,13 @@ public class PoiFiltersHelper {
} }
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) {
query.close(); query.close();
}
query = conn.rawQuery("SELECT " + FILTER_COL_ID + ", " + FILTER_COL_NAME + "," + FILTER_COL_FILTERBYNAME + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ query = conn.rawQuery("SELECT " + FILTER_COL_ID + ", " + FILTER_COL_NAME + "," + FILTER_COL_FILTERBYNAME + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
FILTER_NAME, null); FILTER_NAME, null);
if (query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
String filterId = query.getString(0); String filterId = query.getString(0);
if (map.containsKey(filterId)) { if (map.containsKey(filterId)) {
@ -507,8 +509,10 @@ public class PoiFiltersHelper {
} }
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) {
query.close(); query.close();
} }
}
return list; return list;
} }

View file

@ -216,6 +216,7 @@ public class TravelDbHelper {
query += ") "; query += ") ";
if (params.size() > 0) { if (params.size() > 0) {
SQLiteCursor cursor = conn.rawQuery(query, params.toArray(new String[params.size()])); SQLiteCursor cursor = conn.rawQuery(query, params.toArray(new String[params.size()]));
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
WikivoyageSearchResult rs = new WikivoyageSearchResult(); WikivoyageSearchResult rs = new WikivoyageSearchResult();
@ -230,6 +231,7 @@ public class TravelDbHelper {
cursor.close(); cursor.close();
} }
} }
}
List<WikivoyageSearchResult> list = new ArrayList<>(groupSearchResultsByCityId(res)); List<WikivoyageSearchResult> list = new ArrayList<>(groupSearchResultsByCityId(res));
sortSearchResults(searchQuery, list); sortSearchResults(searchQuery, list);
@ -252,6 +254,9 @@ public class TravelDbHelper {
} }
String LANG_WHERE = " WHERE " + ARTICLES_COL_LANG + " = '" + language + "'"; String LANG_WHERE = " WHERE " + ARTICLES_COL_LANG + " = '" + language + "'";
SQLiteCursor cursor = conn.rawQuery(POP_ARTICLES_TABLE_SELECT + LANG_WHERE, null); SQLiteCursor cursor = conn.rawQuery(POP_ARTICLES_TABLE_SELECT + LANG_WHERE, null);
if(cursor == null) {
return popularArticles;
}
// read popular articles // read popular articles
List<PopularArticle> popReadArticlesOrder = new ArrayList<>(); List<PopularArticle> popReadArticlesOrder = new ArrayList<>();
List<PopularArticle> popReadArticlesLocation = new ArrayList<>(); List<PopularArticle> popReadArticlesLocation = new ArrayList<>();
@ -327,6 +332,7 @@ public class TravelDbHelper {
bld.append(")"); bld.append(")");
cursor = conn.rawQuery(bld.toString(), null); cursor = conn.rawQuery(bld.toString(), null);
Map<Long, TravelArticle> ts = new HashMap<Long, TravelArticle>(); Map<Long, TravelArticle> ts = new HashMap<Long, TravelArticle>();
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
TravelArticle travelArticle = readArticle(cursor); TravelArticle travelArticle = readArticle(cursor);
@ -334,6 +340,7 @@ public class TravelDbHelper {
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
}
return ts; return ts;
} }
@ -461,7 +468,7 @@ public class TravelDbHelper {
} }
} }
SQLiteCursor cursor = conn.rawQuery(query.toString(), params.toArray(new String[params.size()])); SQLiteCursor cursor = conn.rawQuery(query.toString(), params.toArray(new String[params.size()]));
if (cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
do { do {
WikivoyageSearchResult rs = new WikivoyageSearchResult(); WikivoyageSearchResult rs = new WikivoyageSearchResult();
rs.tripId = cursor.getLong(0); rs.tripId = cursor.getLong(0);
@ -480,8 +487,10 @@ public class TravelDbHelper {
} }
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
if (cursor != null) {
cursor.close(); cursor.close();
} }
}
LinkedHashMap<WikivoyageSearchResult, List<WikivoyageSearchResult>> res = new LinkedHashMap<>(); LinkedHashMap<WikivoyageSearchResult, List<WikivoyageSearchResult>> res = new LinkedHashMap<>();
for (String header : headers) { for (String header : headers) {
WikivoyageSearchResult searchResult = headerObjs.get(header); WikivoyageSearchResult searchResult = headerObjs.get(header);
@ -510,11 +519,13 @@ public class TravelDbHelper {
if (conn != null) { if (conn != null) {
SQLiteCursor cursor = conn.rawQuery(ARTICLES_TABLE_SELECT + " WHERE " + ARTICLES_COL_TRIP_ID + " = ? AND " SQLiteCursor cursor = conn.rawQuery(ARTICLES_TABLE_SELECT + " WHERE " + ARTICLES_COL_TRIP_ID + " = ? AND "
+ ARTICLES_COL_LANG + " = ?", new String[] { String.valueOf(cityId), lang }); + ARTICLES_COL_LANG + " = ?", new String[] { String.valueOf(cityId), lang });
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
res = readArticle(cursor); res = readArticle(cursor);
} }
cursor.close(); cursor.close();
} }
}
return res; return res;
} }
@ -525,11 +536,13 @@ public class TravelDbHelper {
SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_TRIP_ID + " FROM " SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_TRIP_ID + " FROM "
+ ARTICLES_TABLE_NAME + " WHERE " + ARTICLES_COL_TITLE + " = ? AND " + ARTICLES_TABLE_NAME + " WHERE " + ARTICLES_COL_TITLE + " = ? AND "
+ ARTICLES_COL_LANG + " = ?", new String[]{title, lang}); + ARTICLES_COL_LANG + " = ?", new String[]{title, lang});
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
res = cursor.getLong(0); res = cursor.getLong(0);
} }
cursor.close(); cursor.close();
} }
}
return res; return res;
} }
@ -540,6 +553,7 @@ public class TravelDbHelper {
if (conn != null) { if (conn != null) {
SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_LANG + " FROM " + ARTICLES_TABLE_NAME SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_LANG + " FROM " + ARTICLES_TABLE_NAME
+ " WHERE " + ARTICLES_COL_TRIP_ID + " = ?", new String[]{String.valueOf(cityId)}); + " WHERE " + ARTICLES_COL_TRIP_ID + " = ?", new String[]{String.valueOf(cityId)});
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
String baseLang = application.getLanguage(); String baseLang = application.getLanguage();
do { do {
@ -559,6 +573,7 @@ public class TravelDbHelper {
} }
cursor.close(); cursor.close();
} }
}
return res; return res;
} }

View file

@ -278,12 +278,14 @@ public class TravelLocalDataHelper {
try { try {
String query = HISTORY_TABLE_SELECT + " WHERE " + HISTORY_COL_TRAVEL_BOOK + " = ?"; String query = HISTORY_TABLE_SELECT + " WHERE " + HISTORY_COL_TRAVEL_BOOK + " = ?";
SQLiteCursor cursor = conn.rawQuery(query, new String[]{travelBook}); SQLiteCursor cursor = conn.rawQuery(query, new String[]{travelBook});
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
WikivoyageSearchHistoryItem item = readHistoryItem(cursor); WikivoyageSearchHistoryItem item = readHistoryItem(cursor);
res.put(item.getKey(), item); res.put(item.getKey(), item);
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
}
cursor.close(); cursor.close();
} finally { } finally {
conn.close(); conn.close();
@ -380,12 +382,14 @@ public class TravelLocalDataHelper {
try { try {
String query = BOOKMARKS_TABLE_SELECT + " WHERE " + BOOKMARKS_COL_TRAVEL_BOOK + " = ?"; String query = BOOKMARKS_TABLE_SELECT + " WHERE " + BOOKMARKS_COL_TRAVEL_BOOK + " = ?";
SQLiteCursor cursor = conn.rawQuery(query, new String[]{travelBook}); SQLiteCursor cursor = conn.rawQuery(query, new String[]{travelBook});
if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
res.add(readSavedArticle(cursor)); res.add(readSavedArticle(cursor));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
}
} finally { } finally {
conn.close(); conn.close();
} }