I am trying to fetch data from database and place it in a ListView in android

250 views Asked by At

I want to create a quiz app which contains 1 question and 4options for that I created a database from which the questions and options are fetched to 5 textviews using ListView. I surfed many tutorials and using a page from stackoverflow created it using ArrayList and Cursor but i am getting a error.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
            at com.example.shark.shopapp.merge.getData(merge.java:26)
            at com.example.shark.shopapp.MainActivity.onCreate(MainActivity.java:68)

It is showing in a Cursor.query() line. MainActivity:

setContentView(R.layout.ques_layout);  db=openOrCreateDatabase("check1.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("CREATE TABLE que(ques TEXT,optn1 TEXT,optn2 TEXT,optn3 TEXT,optn4 TEXT)");
ListView l1=(ListView)findViewById(R.id.listView2);
merge mer=new merge(this);
ArrayList<String> data=mer.getData();
l1.setAdapter(new ArrayAdapter<String>(this,R.layout.ques_layout,data));
db.close();

Merge.java:

public merge(Context c){ourContext=c;}
    public ArrayList<String> getData() {
        String[] columns = new String[]{ques, optn1, optn2, optn3, optn4};
        Cursor c2=db.query(DATABASE_TABLE,columns,null,null,null,null,null);
        ArrayList<String> result = new ArrayList<String>();
        int ques = c2.getColumnIndex("_id");
        int optn1 = c2.getColumnIndex("optn1");
        int optn2 = c2.getColumnIndex("optn2");
        int optn3 = c2.getColumnIndex("optn3");
        int optn4 = c2.getColumnIndex("optn4");
        for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) {
            result.add(c2.getString(ques) + c2.getString(optn1) + c2.getString(optn2) + c2.getString(optn3) + c2.getString(optn4));
        }
        db.close();
        return result;
        }
    }
3

There are 3 answers

14
Agapitz On BEST ANSWER

try this :
String strQuery="select option from (select option1 as option from table
union all
select option2 as option from table
union all
select option3 as option from table
union all
select option4 as option from table) as result";
Cursor c2 = db.SelectQuery(strQuery);
String[] from ={"option"};//you can add as many as you need.
in[] to to = {"R.id.TextViewID"};//you can add as many as you need.
SimpleCursorAdapter sca = new SimpleCursorAdapter(youractivityname.this,R.layout.listviewlayout,c2,from,to); yourlistview.setAdapter(sca);

1
laalto On

Your db variable is not initialized in merge.

Assuming you're using SQLiteOpenHelper, call e.g. getWritableDatabase() on the helper object to get an SQLiteDatabase object you can assign to db.

0
Shoeb Siddique On

You can do something like this.

_cursor = _database.rawQuery("select * from "+YOUR_TABLE_NAME, null);

                _cursor.moveToFirst(); 
                while(_cursor.isAfterLast() == false){

                    _idDB =  _cursor.getInt(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_ID));
                    _ivrsDB =  _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_IVRS_NUM));
                    _readingDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_METER_READING)); 
                    _imageDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_IMAGE));
                    _userIdDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_USER_ID));
                    _latDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_LAT));
                    _longDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_LONG));
                    _dateDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_DATE_TIME));
                    _mob1DB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_MOB_FIRST));
                    _mob2DB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_MOB_SECOND));
                    _reasonSelected  = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_REASON));

                    //need to call UPDATE WS
                    updateDataFromDB(context, _idDB , _ivrsDB , _readingDB, _imageDB, _userIdDB , _latDB , _longDB, _dateDB , _mob1DB , _mob2DB
                            ,_reasonSelected);


                    System.out.println("Id from DB=" + _idDB);
                    System.out.println("IVRS NUMBER="+ _ivrsDB);
                    System.out.println("Reading="+ _readingDB);
                    //System.out.println("Image Path="+_cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_IMAGE)));
                    System.out.println("USer ID="+_userIdDB);
                    System.out.println("LAT="+_latDB);
                    System.out.println("LONG="+_longDB);
                    System.out.println("DATE="+ _dateDB);
                    System.out.println("MOb 1="+_mob1DB);
                    System.out.println("Mob 2="+_mob2DB);
                    System.out.println("Reason from db =" + _reasonSelected);




                    _cursor.moveToNext();
                }
                _cursor.close();