android 空指针异常:空对象引用上的Connection.createStatement()

i5desfxk  于 2023-02-02  发布在  Android
关注(0)|答案(1)|浏览(155)

我在Android中使用JDBC完成学校的一个项目(我知道这不是一件好事),我不明白为什么createStatement返回null。我在AsyncTask中执行jdbc代码:

private class Inserisci extends AsyncTask<Void, Void, ResultSet> {

    @Override
    protected ResultSet doInBackground(Void... params) {

        String comando = "SELECT nome FROM prodotti";
        Connection c = null;
        try {
            c = DriverManager.getConnection("jdbc:mysql://192.168.137.1:3306/eshop", "root", "");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Statement s = null;
        try {
            s = c.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        comando = "SELECT nome FROM prodotti";
        try {
            return s.executeQuery(comando);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(ResultSet resultSet) {
        try {
            showMessage(resultSet.getString(1));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

192.168.137.1 是我运行Xampp的计算机的IP(Apache和MySQL打开),端口是3306,用户名和密码是默认值。我正在运行一个简单的查询以打印第一个结果,但我无法获得它。清单中有Internet权限。ShowMessage方法只是一个吐司消息。需要帮助吗?谢谢
这是完整的错误日志

05-15 21:01:55.925 11105-11185/com.quintabi.facchini.foobarmanager E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                     Process: com.quintabi.facchini.foobarmanager, PID: 11105
                                                                                     java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                         at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                         at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                         at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                         at 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)
                                                                                         at java.lang.Thread.run(Thread.java:761)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
                                                                                         at com.quintabi.facchini.foobarmanager.MainActivity$Inserisci.doInBackground(MainActivity.java:128)
                                                                                         at com.quintabi.facchini.foobarmanager.MainActivity$Inserisci.doInBackground(MainActivity.java:114)
                                                                                         at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                         at 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) 
                                                                                         at java.lang.Thread.run(Thread.java:761) 
jm81lzqq

jm81lzqq1#

将代码 Package 在同一个try catch子句中,看起来您无法创建Connection。
试试这个:

private class Inserisci extends AsyncTask<Void, Void, ResultSet> {

@Override
protected ResultSet doInBackground(Void... params) {

    String comando = "SELECT nome FROM prodotti";
    Connection c = null;
    try {
        c = DriverManager.getConnection("jdbc:mysql://192.168.137.1:3306/eshop", "root", "");
        Statement s = null;
        s = c.createStatement();
        comando = "SELECT nome FROM prodotti";
        return s.executeQuery(comando); 
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(ResultSet resultSet) {
    try {
        showMessage(resultSet.getString(1));
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

相关问题