diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index f8b4d168f6..8c19dfbd53 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -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(); } diff --git a/OsmAnd/src/net/osmand/plus/GPXDatabase.java b/OsmAnd/src/net/osmand/plus/GPXDatabase.java index 90aa873e5e..d64e773c1b 100644 --- a/OsmAnd/src/net/osmand/plus/GPXDatabase.java +++ b/OsmAnd/src/net/osmand/plus/GPXDatabase.java @@ -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(); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java index aefeabf041..8bb29014ed 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java @@ -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 st = new HashMap(); - 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(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index 1399dd0f48..63c3b5c75b 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -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(); } diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index 2100657bcf..a84cadc02f 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -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>> map = new LinkedHashMap>>(); - 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; } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java index f1fd2e6046..04f6e1d306 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java @@ -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 popReadArticlesOrder = new ArrayList<>(); List popReadArticlesLocation = new ArrayList<>(); @@ -327,13 +332,15 @@ public class TravelDbHelper { bld.append(")"); cursor = conn.rawQuery(bld.toString(), null); Map ts = new HashMap(); - 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> 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; } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java index e4b2b990b6..32761c7ba2 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java @@ -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(); }