Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException when calling a database
    text
    copied!<p>As the title says I get a NullPointerException when I try to get a list from a database. Here is the code: </p> <pre><code>public List&lt;String&gt; getAllRockBands() { List&lt;String&gt; bandList = new ArrayList&lt;String&gt;(); String selection = BAND_GENRE + "=1"; SQLiteDatabase db = sqLiteHelper.getReadableDatabase(); Cursor cursor = db.query(BANDDATABASE_TABLE,new String[]{BAND_NAME}, selection, null, null, null, BAND_NAME); int index = cursor.getColumnIndex(BAND_NAME); if (cursor.moveToFirst()) { do { String bandName = cursor.getString(index); bandList.add(bandName); } while (cursor.moveToNext()); } return bandList; } </code></pre> <p>` </p> <pre><code>public class RockAllFragment extends SherlockListFragment { private BandDatabaseAdapter mySQLiteAdapter; private List&lt;String&gt; bandList = mySQLiteAdapter.getAllRockBands(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.my_list, null); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // TODO Auto-generated method stub super.onViewCreated(view, savedInstanceState); setListAdapter(new ArrayAdapter&lt;String&gt;(getSherlockActivity(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, bandList)); } } </code></pre> <p>I am still learning to code in this environment, so I guess this is just some beginner mistake?</p> <p><strong>EDIT:</strong> (it's a bit much but you asked) </p> <pre><code>public class BandDatabaseAdapter { private static final String TAG = "BandDatabase"; private static final String BANDDATABASE_NAME = "BAND_DATABASE"; private static final String BANDDATABASE_TABLE = "BAND_TABLE"; private static final int BANDDATABASE_VERSION = 1; public static final String BAND_NAME = "name"; public static final String BAND_GENRE = "genre"; public static final String BAND_POPULAR = "popular"; public static final String BAND_SELECTED = "selected"; private static final String BANDDATABASE_CREATE="CREATE TABLE IF NOT EXISTS " + BANDDATABASE_TABLE + " (_id integer primary key autoincrement, " + BAND_NAME + " TEXT NOT NULL, " + BAND_GENRE + " TEXT NOT NULL, " + BAND_POPULAR + " TEXT NOT NULL, " + BAND_SELECTED + " TEXT NOT NULL)"; private SQLiteHelper sqLiteHelper; private SQLiteDatabase sqLiteDatabase; private final Context context; public BandDatabaseAdapter(Context c) { sqLiteHelper = new SQLiteHelper(c, BANDDATABASE_NAME, null, BANDDATABASE_VERSION); context = c; } public BandDatabaseAdapter openToRead() throws android.database.SQLException { sqLiteDatabase = sqLiteHelper.getReadableDatabase(); return this; } public BandDatabaseAdapter openToWrite() throws android.database.SQLException { sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this; } public void close(){ sqLiteHelper.close(); } public void updateBand(String name, String selected) { ContentValues contentValues = new ContentValues(); contentValues.put(BAND_SELECTED, selected); sqLiteDatabase.update(BANDDATABASE_TABLE, contentValues, "BAND_NAME=" + name, null); close(); } public List&lt;String&gt; getAllRockBands() { List&lt;String&gt; bandList = new ArrayList&lt;String&gt;(); String selection = BAND_GENRE + "=1"; SQLiteDatabase db = sqLiteHelper.getReadableDatabase(); Cursor cursor = db.query(BANDDATABASE_TABLE,new String[]{BAND_NAME}, selection, null, null, null, BAND_NAME); if(cursor!=null) { int index = cursor.getColumnIndex(BAND_NAME); if (cursor.moveToFirst()) { do { String bandName = cursor.getString(index); bandList.add(bandName); } while (cursor.moveToNext()); } } return bandList; } public List&lt;String&gt; getMyList() { List&lt;String&gt; myList = new ArrayList&lt;String&gt;(); String selection = BAND_SELECTED + "=1"; SQLiteDatabase db = sqLiteHelper.getReadableDatabase(); Cursor cursor = db.query(BANDDATABASE_TABLE,new String[]{BAND_NAME}, selection, null, null, null, BAND_NAME); int index = cursor.getColumnIndex(BAND_NAME); if (cursor.moveToFirst()) { do { String bandName = cursor.getString(index); myList.add(bandName); } while (cursor.moveToNext()); } return myList; } public class SQLiteHelper extends SQLiteOpenHelper { public SQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } private boolean doesDatabaseExist(Context context, String dbName) { File dbFile=context.getDatabasePath(dbName); return dbFile.exists(); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub if(!(doesDatabaseExist(context,BANDDATABASE_NAME))) { db.execSQL(BANDDATABASE_CREATE); loadDatabase(); } } /** * Starts a thread to load the database table with bands */ private void loadDatabase() { new Thread(new Runnable() { public void run() { try { loadBands(); } catch (IOException e) { throw new RuntimeException(e); } } }).start(); } //loads a database from a txt file private void loadBands() throws IOException { Log.d(TAG, "Loading bands..."); final Resources resources = context.getResources(); InputStream inputStream = resources.openRawResource(R.raw.bands); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try { String line; while ((line = reader.readLine()) != null) { String[] strings = TextUtils.split(line, "^"); if (strings.length &lt; 4) continue; long id = addBand(strings[0].trim(), strings[1].trim(), strings[2].trim()); if (id &lt; 0) { Log.e(TAG, "unable to add band: " + strings[0].trim()); } } } finally { reader.close(); } Log.d(TAG, "DONE loading words."); } public long addBand(String name, String genre, String popular) { ContentValues initialValues = new ContentValues(); initialValues.put(BAND_NAME, name); initialValues.put(BAND_GENRE, genre); initialValues.put(BAND_POPULAR, popular); initialValues.put(BAND_SELECTED, "0"); return sqLiteDatabase.insert(BANDDATABASE_TABLE, null, initialValues); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + BANDDATABASE_TABLE); // Create tables again onCreate(db); } } } </code></pre> <p>Additional info: Some people have told me to use SimpleCursorAdapter, but it says it is deprecated. Also, note that my list has over 150k rows, tho I will greatly lessen that if the need arises. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload