我試圖從mysql數據庫中提取數據,但我的代碼一直在拋出nullpointerexception
並認爲它是因爲沒有數組正在創建,我做錯了什麼? 這是代碼: - 謝謝!使用JDBC從數據庫中提取數據並創建數組
public ArrayList<String> getRow(String quEry, String db){
String url = "jdbc:mysql://localhost:3306/"+db;
try {
conn = DriverManager.getConnection(url, "root", "");
} catch (SQLException e) {
}
try{
query = conn.prepareStatement(quEry);
}catch(Exception e){
}
try {
getRow_Method_Result = query.executeQuery();
} catch (SQLException e) {
}
try{
while(getRow_Method_Result.next()){
row_result.add(getRow_Method_Result.getString(1));
row_result.add(getRow_Method_Result.getString(2));
row_result.add(getRow_Method_Result.getString(3));
row_result.add(getRow_Method_Result.getString(4));
}
}catch(Exception e){
}
return row_result;
}
public static void main(String[] args) {
ConectorBaseDatos OBJ_testGetRowMethod = new ConectorBaseDatos();
ArrayList<String> rowListResult_ofMethos = OBJ_testGetRowMethod.getRow("SELECT * FROM `arbitros`", "fifa");
System.out.println(rowListResult_ofMethos.toString());
}
我試圖創建此作爲String[][] array
但由於這是一個有點複雜決定先創建它作爲一個ArrayList那麼這個結果將其插入ArrayList<ArrayList<String>>
但只是停留HERE
發佈您的錯誤堆棧。堆棧應該告訴你哪一行代碼拋出了'NullPointerException'。另外,'rowListResult_ofMethos.toString()'不會發出任何有用的,可讀的字符串。 – wns349
@ wns349,感謝,這裏是堆棧:在螺紋 '異常 「主」 顯示java.lang.NullPointerException \t在modelo.ConectorBaseDatos.main(ConectorBaseDatos.java:422) ' 其是這行代碼: '\t \t \t \t \t \t \t \t的System.out.println(rowListResult_ofMethos.toString());' – user3363537
@ wns349我已經得到這個錯誤,但只是不明白爲什麼,我現在學習使用調試器,但仍然在學習過程中可能會出現什麼問題? – user3363537