我正在嘗試向Java中的Sql Server添加記錄。在此功能,程序給出這樣的錯誤:java.lang.NullPointerException **錯誤?
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」)。(「語句= con.createstatement」 ) – Rob
全堆棧跟蹤請?不要只打印異常消息,而是隱藏真正發生的事情。爲了這個目的,使用e.printStackTrace() – pcalcao