-2

Been trying to retrieve data from database, my database has values and I don't know which causes this error.

public void popList(String date, String time, String type, String game, String place){
    Cursor data = databaseHelper.getReports(date, time, type, game, place);
    while (data.moveToNext()) {
        HashMap<String, String> datax = new HashMap<>();
        datax.put("id", (data.getString(data.getColumnIndex("betid"))));
        datax.put("betnumber", (data.getString(data.getColumnIndex("betnum"))));
        datax.put("betamount", (data.getString(data.getColumnIndex("betamt"))));
        mData3.add(datax);
    }
    bettorAdapter = new MyAdapter(mData3);
    listViewx.setAdapter(bettorAdapter);
    bettorAdapter.notifyDataSetChanged();
}


    public Cursor getReports(String date, String time, String type, String game, String place){
    Cursor data=null;
    try {
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM reports WHERE date='"+date+"' AND time ='"+time+"' AND game='"+game+"' AND type='"+type+"' AND lugar='"+place+"'";
    data = db.rawQuery(query,null);
    }catch (Exception e){
        System.out.println(e);
    }
    return data;
}
kwestionable
  • 496
  • 2
  • 8
  • 23
  • is there any exception from `getReports()`? – Priyanka Jul 24 '19 at 08:06
  • yes, here it is @Priyankagb `java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.johnlim.butadaapplistview.DatabaseHelper.getReports(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference` – kwestionable Jul 24 '19 at 08:08
  • 1
    your `databaseHelper` is null. assign it before use it – Priyanka Jul 24 '19 at 08:09

1 Answers1

0

You need to instantiate/construct the databaseHelper object as it will be null if it is only declared. That is you need to use an equivalent of the line :-

databaseHelper = new DatabaseHelper(appropriate_values_for the_constructor);

Where appropriate_values_for the_constructor are as it says the appropriate value(s) for the constructor, typically just the context.

The above would be used before calling the poplist method. Typically databaseHelper would be declared at the class level and the above line would be set as soon as, or shortly after, the context were available. In an activity this would be in the onCreate method probably just after the ContentView has been set.

The following is an example where databaseHelper is named mDBHlpr and the constructor just needs the one value to be passed, a valid the Context (e.g. for an activity this). :-

public class MainActivity extends AppCompatActivity {

    DatabaseHelper mDBHlpr; //<<<<<<<<<< Declares the mDBHlpr instance, it will be null until instantiated

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHlpr = new DatabaseHelper(this); //<<<<<<<<<<< Instantiates the Database Helper instance named mDBHlpr
        .......... other code
    }
    .......... other code
}
MikeT
  • 51,415
  • 16
  • 49
  • 68