gpt4 book ai didi

java.lang.NullPointerException** 错误?

转载 作者:行者123 更新时间:2023-12-01 06:36:31 26 4
gpt4 key购买 nike

我正在尝试用 Java 将记录添加到 Sql Server。在此函数中,程序给出如下错误:

Connected
1
2
Error java.lang.NullPointerException

这是输出..

函数如下:

public class DB {

Connection con = null;
Statement stmt Nnull ;
component cmp = new Component();

public long pSave(Component cmp) {
String i = cmp.getI();
String s = cmp.getS();
String a = cmp.getA();
int t = cmp.getT();
int c = cmp.getC();

System.out.println("1");

try {
System.out.println("2");
stmt = con.createStatement();

System.out.println("3");
String SQL =
"INSERT INTO kisi (cl1,cl2,cl3,cl4,cl5) "
+ "VALUES(" + i + "," + s + "," + a + "," + c + "," + t + ")";

System.out.println("4");
stmt.executeUpdate(SQL);

System.out.println("Success");

return 1;
} catch(Exception e) {
System.out.println("Error " + e);
return 0;
}
}

}

最佳答案

Connection con = null;
......
stmt = con.createStatement();
^

连接未初始化。您必须连接到数据库才能检索数据。

关于java.lang.NullPointerException** 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7995382/

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