gpt4 book ai didi

java - 空对象引用上的 NullPointerException : Connection. createStatement()

转载 作者:行者123 更新时间:2023-11-29 18:59:12 24 4
gpt4 key购买 nike

我正在 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,用户名和密码是默认的。我正在运行一个简单的查询来打印第一个结果,但我无法得到它。 list 中有互联网许可。 ShowMessage 方法只是一个 toast 消息。有什么帮助吗?谢谢

这是完整的错误日志

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) 

最佳答案

将代码包装在同一个 try catch 子句中,看起来您无法创建连接。

试试这个:

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();
}
}
}

关于java - 空对象引用上的 NullPointerException : Connection. createStatement(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43986985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com