1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at com.demo.parsing.MainActivity.main(MainActivity.java:47)

Writer out = new FileWriter(new File(args[0])); (第 47 行)

我是 JAVA 的新手。我一直在尝试这段代码,但找不到这个arrayoutofboundexception。在这段代码中,我试图从本地 XML 文件中解析一些数据并将其写入一个新文件中。文件已创建但没有文本并出现上述异常

package com.demo.parsing;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class MainActivity {

//private static OutputStreamWriter out;


public static void main(String[] args) throws FileNotFoundException {
    FileInputStream f0 = new FileInputStream("/home/kailash/workspace/paarsing/pustaka-feed.xml");

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Document feed = builder.parse(f0);

        NodeList items = feed.getElementsByTagName("item");

        List<String> urls = new LinkedList<String>();

        for (int i = 0; i < items.getLength(); i++) {
            Element item = (Element) items.item(i);
            Element description = (Element) item.getElementsByTagName(
                    "description").item(0);
            urls.addAll(getImages(builder,    description.getTextContent()));
        }

 Writer out = new FileWriter(new File(args[0]));

        for (String url : urls) {
            out.write(url + "\n");
        }

        out.close();

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static List<String> getImages(DocumentBuilder builder, String content) throws SAXException, IOException {
    List<String> urls = new LinkedList<String>();

    for (org.jsoup.nodes.Element img :   Jsoup.parse(content).getElementsByTag("img")) {
        urls.add(img.attributes().get("src"));
    }

    return urls;
}
}
4

1 回答 1

3

正如您所说,您是新手并且不知道 ArrayIndexOutOfBoundsException 是什么,最好阅读有关ArrayIndexOutOfBoundsException的信息,因为这是您将来将面临的最常见的异常之一。

现在关于您的问题,它在第 47 行抛出 ArrayIndexOutOfBoundsException

正如你所指出的,Line 47

    Writer out = new FileWriter(new File(args[0]));

仔细查看异常跟踪

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at com.demo.parsing.MainActivity.main(MainActivity.java:47)

痕迹告诉你

  1. 异常:ArrayIndexOutOfBoundsException
  2. 发生此异常的索引:0
  3. 抛出异常的行:MainActivity.java:47(这就是为什么我问第 47 行是什么)

^告诉你上面的信息,因为你是一个新手,这个描述可能在不久的将来对你有帮助。

所以现在回到你的陈述

Writer out = new FileWriter(new File(args[0]));

args[0],它代表命令行参数(见主方法定义)

当您尝试运行应用程序时,您是否传递了任何命令行参数?

传递命令行参数

通过 DOS:java nameofclassfile arg1 arg2 ......

通过 Eclipse :right click java file -> run as-run configurations, in the dialog navigate to Arguments tab and enter your command line argument under the Program Arguments text Box and hit run

于 2012-11-01T06:22:12.283 回答