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());
} }
query.close(); if (query != null) {
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());
} }
query.close(); if (query != null) {
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);
} }
query.close(); if (query != null) {
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,7 +401,9 @@ public class SearchHistoryHelper {
entries.add(e); entries.add(e);
} while (query.moveToNext()); } while (query.moveToNext());
} }
query.close(); if(query != null) {
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 {
} }
} }
query.close(); if(query != null) {
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());
} }
query.close(); if(query != null) {
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);
} }
query.close(); if(query != null) {
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());
} }
query.close(); if(query != null) {
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());
} }
query.close(); if(query != null) {
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());
} }
query.close(); if(query != null) {
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,7 +509,9 @@ public class PoiFiltersHelper {
} }
} while (query.moveToNext()); } while (query.moveToNext());
} }
query.close(); if(query != null) {
query.close();
}
} }
return list; return list;
} }

View file

@ -216,18 +216,20 @@ 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.moveToFirst()) { if (cursor != null) {
do { if (cursor.moveToFirst()) {
WikivoyageSearchResult rs = new WikivoyageSearchResult(); do {
rs.tripId = cursor.getLong(0); WikivoyageSearchResult rs = new WikivoyageSearchResult();
rs.articleTitles.add(cursor.getString(1)); rs.tripId = cursor.getLong(0);
rs.langs.add(cursor.getString(2)); rs.articleTitles.add(cursor.getString(1));
rs.isPartOf.add(cursor.getString(3)); rs.langs.add(cursor.getString(2));
rs.imageTitle = cursor.getString(4); rs.isPartOf.add(cursor.getString(3));
res.add(rs); rs.imageTitle = cursor.getString(4);
} while (cursor.moveToNext()); res.add(rs);
} while (cursor.moveToNext());
}
cursor.close();
} }
cursor.close();
} }
} }
@ -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,13 +332,15 @@ 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.moveToFirst()) { if (cursor != null) {
do { if (cursor.moveToFirst()) {
TravelArticle travelArticle = readArticle(cursor); do {
ts.put(travelArticle.tripId, travelArticle); TravelArticle travelArticle = readArticle(cursor);
} while (cursor.moveToNext()); ts.put(travelArticle.tripId, travelArticle);
} 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,7 +487,9 @@ public class TravelDbHelper {
} }
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); if (cursor != null) {
cursor.close();
}
} }
LinkedHashMap<WikivoyageSearchResult, List<WikivoyageSearchResult>> res = new LinkedHashMap<>(); LinkedHashMap<WikivoyageSearchResult, List<WikivoyageSearchResult>> res = new LinkedHashMap<>();
for (String header : headers) { for (String header : headers) {
@ -509,11 +518,13 @@ public class TravelDbHelper {
SQLiteConnection conn = openConnection(); SQLiteConnection conn = openConnection();
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.moveToFirst()) { if (cursor != null) {
res = readArticle(cursor); if (cursor.moveToFirst()) {
res = readArticle(cursor);
}
cursor.close();
} }
cursor.close();
} }
return res; return res;
} }
@ -525,10 +536,12 @@ 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.moveToFirst()) { if (cursor != null) {
res = cursor.getLong(0); if (cursor.moveToFirst()) {
res = cursor.getLong(0);
}
cursor.close();
} }
cursor.close();
} }
return res; return res;
} }
@ -540,24 +553,26 @@ 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.moveToFirst()) { if (cursor != null) {
String baseLang = application.getLanguage(); if (cursor.moveToFirst()) {
do { String baseLang = application.getLanguage();
String lang = cursor.getString(0); do {
if (lang.equals(baseLang)) { String lang = cursor.getString(0);
res.add(0, lang); if (lang.equals(baseLang)) {
} else if (lang.equals("en")) {
if (res.size() > 0 && res.get(0).equals(baseLang)) {
res.add(1, lang);
} else {
res.add(0, lang); res.add(0, lang);
} else if (lang.equals("en")) {
if (res.size() > 0 && res.get(0).equals(baseLang)) {
res.add(1, lang);
} else {
res.add(0, lang);
}
} else {
res.add(lang);
} }
} else { } while (cursor.moveToNext());
res.add(lang); }
} cursor.close();
} while (cursor.moveToNext());
} }
cursor.close();
} }
return res; return res;
} }

View file

@ -278,11 +278,13 @@ 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.moveToFirst()) { if (cursor != null) {
do { if (cursor.moveToFirst()) {
WikivoyageSearchHistoryItem item = readHistoryItem(cursor); do {
res.put(item.getKey(), item); WikivoyageSearchHistoryItem item = readHistoryItem(cursor);
} while (cursor.moveToNext()); res.put(item.getKey(), item);
} while (cursor.moveToNext());
}
} }
cursor.close(); cursor.close();
} finally { } finally {
@ -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.moveToFirst()) { if (cursor != null) {
do { if (cursor.moveToFirst()) {
res.add(readSavedArticle(cursor)); do {
} while (cursor.moveToNext()); res.add(readSavedArticle(cursor));
} while (cursor.moveToNext());
}
cursor.close();
} }
cursor.close();
} finally { } finally {
conn.close(); conn.close();
} }