【问题标题】:Illegal Argument Exception while Reading XML File in Java在 Java 中读取 XML 文件时出现非法参数异常
【发布时间】:2015-07-11 14:32:14
【问题描述】:

我是在观看视频教程时写的。教程显示了同样的东西,但没有错误!

代码如下:

public static void main(String[] args){
    
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = bf.newDocumentBuilder();

        //says input stream cannot be null
        Document d = db.parse(XmlReader.class.getResourceAsStream("XMLFile.xml")); 

        d.normalize();

当我运行程序时,它说输入流不能为指示语句的语句为空,但是它的 not 为空!。文件路径正确!有什么我想念的吗? eclipse或者XMLfiles什么的有问题吗?

堆栈跟踪:

Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at io.XMLFileReading.main(XMLFileReading.java:27)

【问题讨论】:

  • 发布完整的堆栈跟踪
  • 线程“主”java.lang.IllegalArgumentException 中的异常:在 io.XMLFileReading.main(XMLFileReading.java:27) 的 javax.xml.parsers.DocumentBuilder.parse(Unknown Source) 处 InputStream 不能为空)
  • 这可能会有所帮助:stackoverflow.com/questions/23997031/…
  • 老兄!非常感谢!

标签: java xml eclipse


【解决方案1】:

似乎使用 FileInputStream 可以完成这项工作(而不是 XmlReader.class.getResourceAsStream())。

Document document = dBuilder.parse(new FileInputStream("XMLFile.xml"));

在此处查看类似问题和接受的答案:Cannot work with XML file. InputStream is null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多