在我的项目中,我试图将数据保存到sqllite数据库中,并试图在保存后读取这些数据。我能够将所有数据写入数据库,但是在尝试检索数据时,我将无法启动活动:Kotlin.kotlinNullPointerException错误。我遵循所有建议的android建议,但仍然收到此错误。任何帮助表示赞赏。
使用的语言
科特琳
错误
Unable to start activity ComponentInfo: kotlin.KotlinNullPointerException
代码
noteList = dbHandler!!.readNotes()
阅读代码
fun readNotes(): ArrayList<Notes>
{
var db=readableDatabase
val list:ArrayList<Notes> = ArrayList()
var selectAll = "SELECT * FROM " + TABLE_NAME
var cursor: Cursor = db.rawQuery(selectAll, null)
if(cursor.moveToFirst())
{
do {
var note = Notes()
note.id=cursor.getInt(cursor.getColumnIndex(KEY_ID))
note.noteName = cursor.getString(cursor.getColumnIndex(KEY_NOTE_NAME))
list.add(note)
}
while (cursor.moveToNext())
}
return list
}
已检查步骤
1) Activity is presented in Android Manifest File
2) Data's are stored in the database
3) Text Views castings are done properly
答案 0 :(得分:0)
dbHandler可能为null。使用!断言该对象不为null。检查您是否已初始化dbHandler或发布了整个代码