0

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.

4
  • 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 Commented Mar 6, 2022 at 7:03
  • what do i need to do to save it in cursor? Commented Mar 6, 2022 at 7:18
  • full database's data you have to put in cursor .. in your code you are reading from cursor where cursor is null. Commented Mar 6, 2022 at 7:26
  • you may take help from here github.com/Niajnm/Android-E-ITmedi/blob/master/app/src/main/… Commented Mar 6, 2022 at 7:27

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I fixed the error by opening the database in readBase()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.