Home > Mobile >  error when reading the database using cursor
error when reading the database using cursor

Time:03-06

when I try to return the readBase() method to ArrayAdapter. I get a NullPointerException cursor error in the manager class.

Main Activity

public class Price Product Activity{
    private Manager manager;
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prise_product);
        manager = new Manager(this);
        spinner=(Spinner) findViewById(R.id.spinner);
        spinnerAdapter();

    }
@Override
protected void onResume() {
    super.onResume();
    manager.openBase();

}

public void spinnerAdapter(){
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
           android.R.layout.simple_spinner_item,
            manager.readBase());
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

}

Manager public class Manager {

    public Manager(Context context){
        this.context = context;
        helper=new Helper(context);
    }

public void openBase(){
    db = helper.getWritableDatabase();
}


public void insertBase(String name, Double gram){
    ContentValues contentValues = new ContentValues();
    contentValues.put(Constants.NAME, name);
    contentValues.put(Constants.GRAM, gram);
    db.insert(Constants.TABLE_NAME, null, contentValues);
}

public List<String> readBase(){
    List<String> tempList = new ArrayList<>();
    Cursor cursor = db.query(Constants.TABLE_NAME, null, null,
            null, null, null, null);
    while (cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndexOrThrow
                (Constants.NAME));
        /*String gram = cursor.getString(cursor.getColumnIndexOrThrow
                (Constants.GRAM));*/
        tempList.add(name);
        cursor.close();
    }

    return tempList;
}



}

error code

Caused by: 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.sweet.base.Manager.readBase(Manager.java:35)

For many, the answer is that they have not opened the database. However, I opened the db.

CodePudding user response:

problem is in this line Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null); you have to read data from database and store in cursor .. but you are setting data by cursor

     public void  Cursor dsiplayData()  {
            SQLiteDatabase sqLiteDatabase = openBase()
            return sqLiteDatabase.rawQuery("SELECT * FROM  TABLE_NAME ", null)
        }

public List<String> readBase(){
    List<String> tempList = new ArrayList<>();
    Cursor cursor =  dsiplayData()
    while (cursor.moveToNext()){
        String name =  cursor.getString(0); //if you want to read mode data from database you have to create a data model class
       
        tempList.add(name);
       cursor.close();
    }

    return tempList;
}
  • Related