mercredi 27 mai 2015

Really ODD 'NullPointerException' in landscape orientation - ANDROID

I am experiencing a very weird 'NullPointerException' when I run the following SQLite query in my Android application:

// Get a list of all the City or Town names in the database - Return ONLY UNIQUE values in the list.
public ArrayList<String> getStanCityTownUniqueArrayList() {

    // [1] Get WRITE access to the database by creating the 'ssDB_Write' object...
    SQLiteDatabase stanSQLiteDb = getReadableDatabase();           // Get 'READ' and 'WRITE' access to the SQL database.

    // [2] Select the 'City Town' (i.e. SONGS_COLUMN_TITLE) and 'Date Created' (i.e. SONGS_COLUMN_DATE) data columns from the Database Table 'SS_DB_Table'. \\
    String[] cols = { STAN_KEY_CITY_TOWN };

    // [3] Run the 'SELECT' SQL Database Query to retrieve the relevant data from the Database Table 'SS_DB_Table'...
    Cursor crsr = stanSQLiteDb.query(STAN_RECORDS_TABLE, cols, null, null, null, null, STAN_KEY_CITY_TOWN);

    // [4] Create a new ArrayList from the database entries and maintain it in the 'SongRow' class form...
    ArrayList<String> cityTownList = new ArrayList<>();

    // [5] Navigate through the Database with a while loop and in so-doing list all the database entries...
    if (crsr.moveToFirst()) {
        do {

            // [5.1] Get the results of the data located in the appropriate columns of the Database Table (i.e. 'SONGS_TABLE')...
            int cityTown = crsr.getColumnIndex(STAN_KEY_CITY_TOWN);

            // [5.2] Add all the data from the Cursor to the newly created ArrayList (i.e. 'Act_Song_List').
            cityTownList.add(crsr.getString(cityTown));

        } while (crsr.moveToNext());
    }

    // [6] Close the Cursor and Database to prevent resource leaks.
    crsr.close();
    stanSQLiteDb.close();

    // [7] Get only the unique values for the 'allCityTownsData' ArrayList.
    TreeSet<String> noDupCityTownSet = new TreeSet<>();
    noDupCityTownSet.addAll(cityTownList);

    // [8] Return the ArrayList once the entire Database has been run-through (i.e. read by the Database Cursor)...
    return new ArrayList<>(noDupCityTownSet);
}

...this very same code works 100% perfectly in portrait orientation, and ONLY fails (force-closes app) when executed in landscape mode. I have, by a process of cancellation/elimination, ruled out all causes of this problem - and am 100% sure this method (pasted above) is the cause of the problem.

I have another SQLite query that is very similar to the above query with the only difference being that I query a different column in the database in the query below:

  // Get a list of all the Establishment Type names from the database - Return ONLY UNIQUE values in the list.
public ArrayList<String> getStanEstablishmentTypeUniqueArrayList() {

    // [1] Get WRITE access to the database by creating the 'ssDB_Write' object...
    SQLiteDatabase stanSQLiteDb = getReadableDatabase();           // Get 'READ' and 'WRITE' access to the SQL database.

    // [2] Select the 'City Town' (i.e. SONGS_COLUMN_TITLE) and 'Date Created' (i.e. SONGS_COLUMN_DATE) data columns from the Database Table 'SS_DB_Table'. \\
    String[] cols = { STAN_KEY_EST_TYPE };

    // [3] Run the 'SELECT' SQL Database Query to retrieve the relevant data from the Database Table 'SS_DB_Table'...
    Cursor crsr = stanSQLiteDb.query(STAN_RECORDS_TABLE, cols, null, null, null, null, STAN_KEY_EST_TYPE);

    // [4] Create a new ArrayList from the database entries and maintain it in the 'SongRow' class form...
    ArrayList<String> establishmentTypeList = new ArrayList<>();

    // [5] Navigate through the Database with a while loop and in so-doing list all the database entries...
    if (crsr.moveToFirst()) {
        do {

            // [5.1] Get the results of the data located in the appropriate columns of the Database Table (i.e. 'SONGS_TABLE')...
            int estType = crsr.getColumnIndex(STAN_KEY_EST_TYPE);

            // [5.2] Add all the data from the Cursor to the newly created ArrayList (i.e. 'Act_Song_List').
            establishmentTypeList.add(crsr.getString(estType));

        } while (crsr.moveToNext());
    }

    // [6] Close the Cursor and Database to prevent resource leaks.
    crsr.close();
    stanSQLiteDb.close();

    // [7] Get only the unique values for the 'allCityTownsData' ArrayList.
    TreeSet<String> noDupEstTypeSet = new TreeSet<>();
    noDupEstTypeSet.addAll(establishmentTypeList);

    // [8] Return the ArrayList once the entire Database has been run-through (i.e. read by the Database Cursor)...
    return new ArrayList<>(noDupEstTypeSet);
}

The second query pasted above works absolutely perfectly in both landscape and portrait modes (orientations). This is what stumbles me in finding solution to the problem. Everything is exactly the same. This very same method 'getStanCityTownUniqueArrayList()' works perfectly in portrait, but not in landscape mode - I have no idea why. Please help.

Thanks in advance, Shore-T.

Aucun commentaire:

Enregistrer un commentaire