java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference to select sqlite

前端 未结 3 1135
我在风中等你
我在风中等你 2021-01-01 02:02

I am a newbie of android world. I have a problem of the coding. It was just a tiny error buy i dont know it doesnt work even i change others method but the error still the s

相关标签:
3条回答
  • 2021-01-01 02:31

    Initialize the DatabaseHelper:

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.surveyinstruction);
    
        DatabaseHelper db = new DatabaseHelper(this);
    
        //...
    }
    
    0 讨论(0)
  • 2021-01-01 02:33

    I had a similar problem with getId from the database. I found that I was not taking the database from mainAcitivity.java.

        public adapter(Context mCtx, int listLayoutRes, List<employee> employeeList,SQLiteDatabase mDatabase) {
        super(mCtx, listLayoutRes, employeeList);
    
        this.mCtx = mCtx;
        this.listLayoutRes = listLayoutRes;
        this.employeeList = employeeList;
        this.mDatabase = mDatabase;
    }
    

    and using this constructor from mainAcitivity

        adpt= new adapter(this,R.layout.row,employeeList,mdatabase);
    

    solved my problem. I hope this helps.

    0 讨论(0)
  • 2021-01-01 02:50

    You have not initialized your reference to DatabaseHelper myDb;. Thats why the nullpointer exception.

    0 讨论(0)
提交回复
热议问题