我得到一個空指針異常。我不知道我爲什麼得到它的原因?這是我得到例外的句子「perst = conn.preparedstatemt(sql)」。準備好的語句中的空指針異常
conn = DBHandler.getDBConnection();
String nextOccid = "";
sql = "select nextOccId from ColdStorage.master_ids";
prest = conn.prepareStatement(sql);
ResultSet rs5 = prest.executeQuery();
while (rs5.next()) {
nextOccid = rs5.getString("nextOccId");
}
我有一個dbhandler文件,其中有連接字符串,其代碼我已附加在這裏。
public static Connection getDBConnection() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Connection conn = null;
String strUserID, strUserPwd, strURL = null;
try {
Class.forName("com.mysql.jdbc.Driver");
strUserID = "root";
strUserPwd = "";
strURL = "jdbc:mysql://localhost:3306/ColdStorage? zeroDateTimeBehavior=convertToNull";
conn = DriverManager.getConnection(strURL, strUserID, strUserPwd);
你在哪一行得到異常?也不要只告訴行號,請確切指出哪一行。完整的堆棧跟蹤將非常有用 –
看起來'conn'爲空(當DriverManager無法獲得連接時可能發生這種情況)。 – Thilo
@Thilo我該如何糾正? – Divyang