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$
FAVOURITE_TABLE_NAME, null);
cachedFavoritePoints.clear();
if (query.moveToFirst()) {
if (query != null && query.moveToFirst()) {
do {
String name = query.getString(0);
String cat = query.getString(1);
@ -748,7 +748,9 @@ public class FavouritesDbHelper {
}
} while (query.moveToNext());
}
query.close();
if (query != null) {
query.close();
}
} finally {
db.close();
}

View file

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

View file

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

View file

@ -213,13 +213,15 @@ public class MapMarkersDbHelper {
if (db != null) {
try {
SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT, null);
if (query.moveToFirst()) {
if (query != null && query.moveToFirst()) {
do {
MapMarkersGroup group = readGroup(query);
res.put(group.getId(), group);
} while (query.moveToNext());
}
query.close();
if(query != null) {
query.close();
}
} finally {
db.close();
}
@ -392,10 +394,12 @@ public class MapMarkersDbHelper {
if (db != null) {
try {
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);
}
query.close();
if(query != null) {
query.close();
}
} finally {
db.close();
}
@ -411,14 +415,16 @@ public class MapMarkersDbHelper {
try {
SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ACTIVE + " = ?",
new String[]{String.valueOf(1)});
if (query.moveToFirst()) {
if (query != null && query.moveToFirst()) {
do {
MapMarker marker = readItem(query);
markers.put(marker.id, marker);
nextKeys.add(marker.nextKey);
} while (query.moveToNext());
}
query.close();
if(query != null) {
query.close();
}
} finally {
db.close();
}
@ -570,12 +576,14 @@ public class MapMarkersDbHelper {
try {
SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ACTIVE + " = ?",
new String[]{String.valueOf(0)});
if (query.moveToFirst()) {
if (query != null && query.moveToFirst()) {
do {
markers.add(readItem(query));
} while (query.moveToNext());
}
query.close();
if(query != null) {
query.close();
}
} finally {
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$
CATEGORIES_NAME, null);
Map<String, Map<PoiCategory, LinkedHashSet<String>>> map = new LinkedHashMap<String, Map<PoiCategory, LinkedHashSet<String>>>();
if (query.moveToFirst()) {
if (query != null && query.moveToFirst()) {
do {
String filterId = query.getString(0);
if (!map.containsKey(filterId)) {
@ -492,11 +492,13 @@ public class PoiFiltersHelper {
}
} 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$
FILTER_NAME, null);
if (query.moveToFirst()) {
if (query != null && query.moveToFirst()) {
do {
String filterId = query.getString(0);
if (map.containsKey(filterId)) {
@ -507,7 +509,9 @@ public class PoiFiltersHelper {
}
} while (query.moveToNext());
}
query.close();
if(query != null) {
query.close();
}
}
return list;
}

View file

@ -216,18 +216,20 @@ public class TravelDbHelper {
query += ") ";
if (params.size() > 0) {
SQLiteCursor cursor = conn.rawQuery(query, params.toArray(new String[params.size()]));
if (cursor.moveToFirst()) {
do {
WikivoyageSearchResult rs = new WikivoyageSearchResult();
rs.tripId = cursor.getLong(0);
rs.articleTitles.add(cursor.getString(1));
rs.langs.add(cursor.getString(2));
rs.isPartOf.add(cursor.getString(3));
rs.imageTitle = cursor.getString(4);
res.add(rs);
} while (cursor.moveToNext());
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
WikivoyageSearchResult rs = new WikivoyageSearchResult();
rs.tripId = cursor.getLong(0);
rs.articleTitles.add(cursor.getString(1));
rs.langs.add(cursor.getString(2));
rs.isPartOf.add(cursor.getString(3));
rs.imageTitle = cursor.getString(4);
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 + "'";
SQLiteCursor cursor = conn.rawQuery(POP_ARTICLES_TABLE_SELECT + LANG_WHERE, null);
if(cursor == null) {
return popularArticles;
}
// read popular articles
List<PopularArticle> popReadArticlesOrder = new ArrayList<>();
List<PopularArticle> popReadArticlesLocation = new ArrayList<>();
@ -327,13 +332,15 @@ public class TravelDbHelper {
bld.append(")");
cursor = conn.rawQuery(bld.toString(), null);
Map<Long, TravelArticle> ts = new HashMap<Long, TravelArticle>();
if (cursor.moveToFirst()) {
do {
TravelArticle travelArticle = readArticle(cursor);
ts.put(travelArticle.tripId, travelArticle);
} while (cursor.moveToNext());
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
TravelArticle travelArticle = readArticle(cursor);
ts.put(travelArticle.tripId, travelArticle);
} while (cursor.moveToNext());
}
cursor.close();
}
cursor.close();
return ts;
}
@ -461,7 +468,7 @@ public class TravelDbHelper {
}
}
SQLiteCursor cursor = conn.rawQuery(query.toString(), params.toArray(new String[params.size()]));
if (cursor.moveToFirst()) {
if (cursor != null && cursor.moveToFirst()) {
do {
WikivoyageSearchResult rs = new WikivoyageSearchResult();
rs.tripId = cursor.getLong(0);
@ -480,7 +487,9 @@ public class TravelDbHelper {
}
} while (cursor.moveToNext());
}
cursor.close();
if (cursor != null) {
cursor.close();
}
}
LinkedHashMap<WikivoyageSearchResult, List<WikivoyageSearchResult>> res = new LinkedHashMap<>();
for (String header : headers) {
@ -509,11 +518,13 @@ public class TravelDbHelper {
SQLiteConnection conn = openConnection();
if (conn != null) {
SQLiteCursor cursor = conn.rawQuery(ARTICLES_TABLE_SELECT + " WHERE " + ARTICLES_COL_TRIP_ID + " = ? AND "
+ ARTICLES_COL_LANG + " = ?", new String[]{String.valueOf(cityId), lang});
if (cursor.moveToFirst()) {
res = readArticle(cursor);
+ ARTICLES_COL_LANG + " = ?", new String[] { String.valueOf(cityId), lang });
if (cursor != null) {
if (cursor.moveToFirst()) {
res = readArticle(cursor);
}
cursor.close();
}
cursor.close();
}
return res;
}
@ -525,10 +536,12 @@ public class TravelDbHelper {
SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_TRIP_ID + " FROM "
+ ARTICLES_TABLE_NAME + " WHERE " + ARTICLES_COL_TITLE + " = ? AND "
+ ARTICLES_COL_LANG + " = ?", new String[]{title, lang});
if (cursor.moveToFirst()) {
res = cursor.getLong(0);
if (cursor != null) {
if (cursor.moveToFirst()) {
res = cursor.getLong(0);
}
cursor.close();
}
cursor.close();
}
return res;
}
@ -540,24 +553,26 @@ public class TravelDbHelper {
if (conn != null) {
SQLiteCursor cursor = conn.rawQuery("SELECT " + ARTICLES_COL_LANG + " FROM " + ARTICLES_TABLE_NAME
+ " WHERE " + ARTICLES_COL_TRIP_ID + " = ?", new String[]{String.valueOf(cityId)});
if (cursor.moveToFirst()) {
String baseLang = application.getLanguage();
do {
String lang = cursor.getString(0);
if (lang.equals(baseLang)) {
res.add(0, lang);
} else if (lang.equals("en")) {
if (res.size() > 0 && res.get(0).equals(baseLang)) {
res.add(1, lang);
} else {
if (cursor != null) {
if (cursor.moveToFirst()) {
String baseLang = application.getLanguage();
do {
String lang = cursor.getString(0);
if (lang.equals(baseLang)) {
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 {
res.add(lang);
}
} while (cursor.moveToNext());
} while (cursor.moveToNext());
}
cursor.close();
}
cursor.close();
}
return res;
}

View file

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