2

I am trying to get data from my sqlite database. It gives me the error of Attempt to invoke method on null reference. I am trying to store the values using Hashmap.

Here's my code:

 class MyAdapter extends BaseAdapter {
        private ArrayList<HashMap<String, String>> mData;

        public MyAdapter() {
            mData = helper.getAllDataLV();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            return view;


        }
        private class ViewHolder {
            TextView tx_row;
        }
    }


public ArrayList<HashMap<String,String>> getAllDataLV(){
ArrayList<HashMap<String, String>> data = new ArrayList<>();
SQLiteDatabase db = myhelper.getWritableDatabase();

Cursor cs = db.rawQuery("SELECT * FROM bets;",null);

if(cs != null && cs.moveToFirst()){
    do{
        HashMap<String, String> q = new HashMap<>();
        q.put("betnumber",  cs.getString(cs.getColumnIndex("betnumber")));
        data.add(q);
    }while(cs.moveToNext());
}
System.out.println(data);

return data;
}

The error is in the getAllDataLV() function.

Attempt to invoke virtual method 'java.util.ArrayList com.example.zian.myDbAdapter.getAllDataLV()' on a null object reference

linken
  • 21
  • 2

0 Answers0