java - Android Cursor NPE dumpCursorToString returns values -


as title says, i'm experiencing nullpointerexception occurs @ first getstring() in below method. debugging have used databaseutils.dumpcursortostring() view cursor , there values within cursor between if statement , do statement, changes. use same code in method in separate activity , works flawlessly.

public arraylist<string> getmusic() {     helper = new dbhelper(context);     db = helper.getreadabledatabase();      string[] cols = new string[]{music_id, music_country, music_blues, music_rock, music_metal,             music_hiphop, music_classical, music_punk, music_techno, music_jazz, music_other};      cursor c = db.query(table_music_int, cols, null, null, null, null, null);      int count = c.getcount();     log.v("interest cursor ", "count: " + count); // log row count         if (c.movetofirst()) {             log.v("interest cursor ", databaseutils.dumpcursortostring(c)); // dump cursor values             {                 musicint.add(c.getstring(0));                 musicint.add(c.getstring(1));                 musicint.add(c.getstring(2));                 musicint.add(c.getstring(3));                 musicint.add(c.getstring(4));                 musicint.add(c.getstring(5));                 musicint.add(c.getstring(6));                 musicint.add(c.getstring(7));                 musicint.add(c.getstring(8));                 musicint.add(c.getstring(9));                 musicint.add(c.getstring(10));             } while (c.movetonext());         } else {             log.e("db_get_data ", " db_get_data == null");         }     c.close();      return musicint; } 

databaseutils.dumpcursortostring(c) output:

01-15 19:57:01.201 6247-6247/us.enyi.enyi d/db open read: successful 01-15 19:57:20.440 6247-6247/us.enyi.enyi v/interest cursor: count: 1 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor: >>>>> dumping cursor android.database.sqlite.sqlitecursor@2f32900f 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor: 0 { 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    _id=1 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    country=null 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    blues=null 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    rock=rock 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    metal=metal 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    hiphop=hiphop 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    classical=classical 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    punk=null 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    techno=techno 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    jazz=null 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor:    musicother=other 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor: } 01-15 19:57:27.897 6247-6247/us.enyi.enyi v/interest cursor: <<<<< 

i've attempted other methods of retrieving data cursor , read countless threads here on within android docs, without finding helpful resolution. don't quite understand why cursor has values c.getstring() fails on npe. assistance appreciated.

solution:

failure initialize musicint caused cursor have put data collected. declared musicint as:

arraylist<string> musicint; 

instead of:

arraylist<string> musicint = new arraylist<>(); 

the lesson ensure initialize variables appropriately. you'll pull hair out did otherwise.

i don't see declare (or initialize) musicint. might what's causing npe.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -