2014-04-19 32 views
0

我試圖從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

+1

發佈您的錯誤堆棧。堆棧應該告訴你哪一行代碼拋出了'NullPointerException'。另外,'rowListResult_ofMethos.toString()'不會發出任何有用的,可讀的字符串。 – wns349

+0

@ 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

+0

@ wns349我已經得到這個錯誤,但只是不明白爲什麼,我現在學習使用調試器,但仍然在學習過程中可能會出現什麼問題? – user3363537

回答

0

我會寫在這裏,因爲這可能會有點長評論。

rowListResult_ofMethodsnull意味着你要OBJ_testGetRowMethod.getRow("SELECT * FROM arbitros ", "fifa")調用返回null

看着你的public ArrayList<String> getRow(String quEry, String db)的方法,我看不到row_result第一次初始化。

所以,你應該找到並確保row_result被初始化的地方。 (可能類似row_result = new ArrayList<String>()某處。)

此外,你應該永遠不要(我會說永遠不會,可能會有所不同,其他人)在發生異常後無所作爲!只要

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){ 
    //Do not leave this blank!!! you must handle exception here ! 
    // At least do e.printStackTrace(); to see exception during development. 
} 

爲連接和查詢到你的數據庫是成功的,我猜上述row_result.add(getRow_Method_Result.getString(1));會拋出一個NullPointerException

不要這樣做。但是這個例外被放棄了,並且沒有采取任何行動,因爲你把那部分留空了。

+0

超凡!是我的部分非常愚蠢的錯誤,但一直在尋找這個小時jeje謝謝! – user3363537

0

你在哪裏聲明ArrayList<String> row_result?(靜態均可) 如果要初始化爲ArrayList<String> row_result=null和你沒有任何結果集返回然後它會扔你NPE。如果你有結果集,那麼NPE將會在第一行中。確保你將它初始化爲ArrayList row_result = new ArrayList();