简体   繁体   English

使用 SQLite 数据库启动应用程序时出现 NullPointerException

[英]NullPointerException on Application startup using SQLite database

i'm relatively new to Kotlin and SQLite, and the application I have made using android studio keeps producing a NullPointerReference error.我对 Kotlin 和 SQLite 比较陌生,我使用 android studio 制作的应用程序不断产生 NullPointerReference 错误。

My app takes input from the user and stores it into the SQLite database.我的应用程序从用户那里获取输入并将其存储到 SQLite 数据库中。 It will then load entries onto the main page.然后它将条目加载到主页上。 When running my app for the 1st time, it produces the following error.第一次运行我的应用程序时,它会产生以下错误。

Logcat逻辑猫

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.reminders/com.example.reminders.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
        at android.content.ContextWrapper.getDatabasePath(ContextWrapper.java:337)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:445)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:415)
        at com.example.reminders.FeedReaderDbHelper.viewReminder(DatabaseHandler.kt:61)
        at com.example.reminders.HistoryActivity.getItemsList(HistoryActivity.kt:37)
        at com.example.reminders.HistoryActivity.setupListofDataintoRecyclerView(HistoryActivity.kt:20)
        at com.example.reminders.MainActivity.onCreate(MainActivity.kt:99)
        at android.app.Activity.performCreate(Activity.java:7963)
        at android.app.Activity.performCreate(Activity.java:7952)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:8167) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 

Relevant code from my MainActivity来自我的 MainActivity 的相关代码

class MainActivity : AppCompatActivity() {

    lateinit var alarmService: AlarmService
    lateinit var historyActivity: HistoryActivity

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
            setContentView(R.layout.history_main)
            createNotificationChannel()
            alarmService = AlarmService(this)
            historyActivity = HistoryActivity()
                                    
                            ...

    }
    historyActivity.setupListofDataintoRecyclerView(this)
}

On startup, i intend for it to check if number of items in the database is > 0. If greater, the app will load the database.在启动时,我打算让它检查数据库中的项目数是否 > 0。如果更大,应用程序将加载数据库。 if not, it will display that there are no entries.如果没有,它将显示没有条目。

Relevant Code from my HistoryActivity我的 HistoryActivity 中的相关代码

    fun setupListofDataintoRecyclerView(context: Context) {
        if (getItemsList().size > 0) {
            tvEmptyHistory.visibility = View.GONE
            rvHistory.visibility = View.VISIBLE
            rvHistory.layoutManager = LinearLayoutManager(context)
            var itemAdapter = HistoryAdapter(context, getItemsList())
            rvHistory.adapter = itemAdapter
        }
        else {
            tvEmptyHistory.visibility = View.VISIBLE
            rvHistory.visibility = View.GONE
        }
    }

    private fun getItemsList(): ArrayList<reminderHistory> {
        //create instance of DatabaseHandler
        val databaseHandler = FeedReaderDbHelper(this)
        //call method to view database
        return databaseHandler.viewReminder()
    }

Relevant code from my FeedReaderDbHelper class我的 FeedReaderDbHelper 类中的相关代码

class FeedReaderDbHelper(context: Context) :
    SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {

                            ...

    fun viewReminder(): ArrayList<reminderHistory> {
        val rHistList: ArrayList<reminderHistory> = ArrayList()
        val selectQuery = "SELECT * FROM $TABLE_CONTACTS"
        val db = this.readableDatabase
        var cursor: Cursor? = null

        try {
            cursor = db.rawQuery(selectQuery, null)
        } catch (e: SQLiteException) {
            db.execSQL(selectQuery)
            return ArrayList()
        }
        var id: Int
        var item: String
        var hour: Int
        var min: Int
        var date: String

        if (cursor.moveToFirst()) {
            do {
                id = cursor.getInt((cursor.getColumnIndex(KEY_ID)))
                item = cursor.getString(cursor.getColumnIndex(KEY_ITEM))
                hour = cursor.getInt(cursor.getColumnIndex(KEY_HOUR))
                min = cursor.getInt(cursor.getColumnIndex(KEY_MIN))
                date = cursor.getString(cursor.getColumnIndex(KEY_DATE))

                val reminder =
                    reminderHistory(id = id, item = item, hour = hour, min = min, date = date)
                rHistList.add(reminder)
            } while (cursor.moveToNext())
        }
        cursor.close()
        return rHistList
    }
                ...

The following error line以下错误行

at com.example.reminders.FeedReaderDbHelper.viewReminder(DatabaseHandler.kt:61)

Refers to the line val db = this.readableDatabase参考行val db = this.readableDatabase

I am unsure if the problem lies with the context being null, (which is of the class) or the database.我不确定问题是否在于上下文为空(属于该类)或数据库。 Please do offer any advice or comments regarding this.请就此提出任何建议或意见。 Thank you.谢谢你。

You are calling historyActivity in the wrong way, Try to open activity using intent.您以错误的方式调用 historyActivity,尝试使用意图打开活动。 Try to implement using following code.尝试使用以下代码实现。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.history_main)
    createNotificationChannel()
    alarmService = AlarmService(this)
    val intent = Intent(this, HistoryActivity::class.java)
    startActivity(intent)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM