2014-05-03 18 views
0

//非常感謝大家,問題出在我的XML文件聲明中。已解決...... DOM ...線程「main」中的異常

,當我試圖運行此代碼我得到這個error..I不知道我錯在哪裏

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at nventaire.test.main(test.java:10) 

>

import org.w3c.dom.*; 

import javax.xml.parsers.*; 

public class test { 
    public static void main(String[] args) throws Exception { 
     DocumentBuilderFactory factory = 
       DocumentBuilderFactory.newInstance(); 
     DocumentBuilder parser = 
       factory.newDocumentBuilder(); 
     Document doc = parser.parse(args[0]); 
     Element racine = doc.getDocumentElement(); 
     NodeList nl = racine.getElementsByTagName("joueur"); 
     for (int i = 0; i < nl.getLength(); ++i) { 
      Element joueur = (Element) nl.item(i); 
      NodeList listedenoms = joueur.getElementsByTagName("nom"); 
      Element nom = (Element) listedenoms.item(0); 
      System.out.println(nom.getFirstChild().getNodeValue()); 
     } 
    } 

thaks

+0

如果:在

java [Program Name] [Whatever your argument is] 

硬編碼你檢查參數(長度,內容等),它顯示了什麼? – AntonH

+0

您在啓動程序時是否提供了任何參數? – iHank

回答

0

這實際上是引發異常的堆棧跟蹤。

我認爲它與args數組有關 - 它可能沒有設置任何元素,因此在訪問時設置爲ArrayIndexOutOfBoundsException。你應該調用一些參數的應用程序,以便args陣列不會是空的,你將能夠訪問它,如:

//Call along with some argument: 
java test firstArgument 

//Then access 
Document doc = parser.parse(args[0]); 
+0

我在同一個文件夾中有一個XML文件。 我試過用java test test.xml 但我得到一個錯誤(沒有這樣的文件或文件夾) – KAMILIA

+0

在哪個文件夾中有'xml'文件? –

+0

@ user3600078您應該擁有除test.class1之外的XML文件或使用絕對路徑。 –

0

您沒有命令行參數。錯誤是:

Document doc = parser.parse(args[0]); 

既然你沒有命令行參數,args是一個空數組和args[0]不存在,引起ArrayIndexOutOfBoundsException

要解決,包括一個參數,當你運行你的程序,或在硬編碼

包括說法:

Document doc = parser.parse("[Whatever your argument is]"); 
+0

如何刪除錯誤....通過什麼我應該取代參數[0]。 – KAMILIA

+0

那麼你需要包括它當你運行你的程序。 –

+0

非常感謝你,但問題是在我的聲明中XML文件。 – KAMILIA

相關問題