java.lang.RuntimeException:使用Kotlin执行doInBackground()时发生错误

时间:2018-06-01 18:00:05

标签: android sql jdbc kotlin

  override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            callPostReq()
            val sm = someTask()
            sm.execute()
        }
inner class someTask(): AsyncTask<String, Int, Array<String>>() {
    override fun doInBackground(vararg params: String): Array<String>?  {
        val conn: Connection= null!!
        val url = "jdbc:mysql://localhost:3306/"
        val dbName = "db-01"
        val driver = "com.mysql.jdbc.Driver"
        val cred = Properties()
        cred.put("username","username")
        cred.put("password","pasword")

        try {
            Class.forName(driver).newInstance()
            conn = DriverManager.getConnection(url,cred)
            println("Connected to the database")
            conn.close()
            println("Disconnected from database")
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}
  

致命异常:AsyncTask#1                     流程:highway.dueamount,PID:15746                     java.lang.RuntimeException:执行doInBackground()时发生错误                         在android.os.AsyncTask $ 3.done(AsyncTask.java:325)                         在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)                         at java.util.concurrent.FutureTask.setException(FutureTask.java:223)                         在java.util.concurrent.FutureTask.run(FutureTask.java:242)                         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)                         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)                         在java.lang.Thread.run(Thread.java:760)                      引起:kotlin.KotlinNullPointerException                         at highway.dueamount.MainActivity $ someTask.doInBackground(MainActivity.kt:60)                         at highway.dueamount.MainActivity $ someTask.doInBackground(MainActivity.kt:57)                         在android.os.AsyncTask $ 2.call(AsyncTask.java:305)                         在java.util.concurrent.FutureTask.run(FutureTask.java:237)                         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)                         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)                         在java.lang.Thread.run(Thread.java:760)

1 个答案:

答案 0 :(得分:0)

您正在尝试解包null。刚删除!!它会起作用

val conn: Connection= null