【问题标题】:java.lang.IllegalArgumentException: InputStream to parse is nulljava.lang.IllegalArgumentException:要解析的 InputStream 为空
【发布时间】:2013-09-13 07:25:46
【问题描述】:

请帮助我了解我的代码有什么问题。I tried from this link for Sample. OS : Windows 7

代码 1:

 DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                            .getClassLoader()
                            .getResourceAsStream("leo/test/digester/student/student.xml"));

例外 1:

true:isFile:isHidden:false    
java.lang.IllegalArgumentException: InputStream to parse is null
at org.apache.commons.digester3.Digester.parse(Digester.java:1621)
at leo.test.digester.student.DigestStudents.digest(DigestStudents.java:41)
at leo.test.digester.student.DigestStudents.main(DigestStudents.java:16)

代码 2:

 File f = new File ("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml");
 System.out.println(f.isFile()+":isFile:isHidden:"+f.isHidden());
 DigestStudents ds = (DigestStudents) digester.parse(f);

例外 2:

true:isFile:isHidden:false
log4j:WARN No appenders could be found for logger (org.apache.commons.digester3.Digester).
log4j:WARN Please initialize the log4j system properly.
java.lang.NullPointerException
    at org.apache.commons.digester3.Digester.getXMLReader(Digester.java:790)
    at org.apache.commons.digester3.Digester.parse(Digester.java:1588)
    at org.apache.commons.digester3.Digester.parse(Digester.java:1557)
    at leo.test.digester.student.DigestStudents.digest(DigestStudents.java:41)
    at leo.test.digester.student.DigestStudents.main(DigestStudents.java:16)

Student.java

package leo.test.digester.student;

public class Student {
private String name;
private String course;

public Student() {
}

public String getName() {
    return name;
}

public void setName(String newName) {
    name = newName;
}

public String getCourse() {
    return course;
}

public void setCourse(String newCourse) {
    course = newCourse;
}
public String toString() {
    return("Name="+this.name + " & Course=" +  this.course);
}
}

student.xml

<?xml version="1.0" encoding="UTF-8"?>
<students>
        <student>
                <name>Java Boy</name>
                <course>JSP</course>
        </student>
        <student>
                <name>Java Girl</name>
                <course>EJB</course>
        </student>
</students>

DigestStudents.java

package leo.test.digester.student;

import java.util.Vector;
import org.apache.commons.digester3.Digester;

public class DigestStudents {
    Vector students;

    public DigestStudents() {
        students= new Vector();
    }

    public static void main(String[] args) {
        DigestStudents digestStudents = new DigestStudents();
        digestStudents.digest();
    }

    private void digest() {
        try {
            Digester digester = new Digester();
            //Push the current object onto the stack
            digester.push(this);

            //Creates a new instance of the Student class
            digester.addObjectCreate( "students/student", Student.class );

            //Uses setName method of the Student instance
            //Uses tag name as the property name
            digester.addBeanPropertySetter( "students/student/name");

            //Uses setCourse method of the Student instance
            //Explicitly specify property name as 'course'
            digester.addBeanPropertySetter( "students/student/course", "course" );

            //Move to next student
            digester.addSetNext( "students/student", "addStudent" );

             File f = new File ("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml");
             System.out.println(f.isFile()+":isFile:isHidden:"+f.isHidden());


            DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                                .getClassLoader()
                                .getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml"));

            //Print the contents of the Vector
            System.out.println("Students Vector "+ds.students);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void addStudent( Student stud ) {
        //Add a new Student instance to the Vector
        students.add( stud );
    }
}

【问题讨论】:

  • 大家好,感谢您的所有帮助。但确切的问题是 JAR 不匹配。最后只尝试了四个罐子,现在它可以工作了。JARs common-collections-2.1.1.jar common-logging-1.0.4 .jar common-beanutils-1.6.1.jar common-digester3.3.0.jar

标签: java xml apache-commons-digester


【解决方案1】:

ClassLoader#getResourceAsStream() 能够定位相对于类路径的“root”的文件。您需要替换此行:

 DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                       .getClassLoader()
                       .getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml"));

 DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                       .getClassLoader()
                       .getResourceAsStream("leo/test/digester/student/student.xml"));

【讨论】:

  • 如果student.xml 文件存在于您的类路径中leo/test/digester/student/,那么它应该可以工作!
  • 感谢您的帮助,但我正在使用 Eclipse IDE 我试图将文件放在一个位置并检查文件是否为空,然后将文件对象传递给消化器,得到一些其他异常。请检查更新问题。
  • 在 Eclipse 中,右键单击您的项目 -> 属性 -> Java 构建路径 -> 源代码。您的“默认输出文件夹”是什么? bin/classes?
  • /RD_Axway。还有文件可用
  • 好的,/RD_Axway里面有leo/test/digester/student/student.xml吗?
【解决方案2】:

我认为这条线有问题:

DigestStudents ds = (DigestStudents) digester.parse(this.getClass().getClassLoader().getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml"));

找不到资源,因此返回null。

【讨论】:

  • 请检查我的异常跟踪文件是否可用!
  • 使用“!”很容易:) 在 getResourceAsStream 中使用绝对路径,如果我错了,请纠正我,是相对于类路径根的。我建议你把它放在你的 src 文件夹中,然后自己解决
  • 或者您可以选择使用 FileInputStream 吗?如果您想坚持使用绝对文件路径
  • 请检查我更新的问题,即使发送文件对象,它也不起作用。
【解决方案3】:

ClassLoader.getResourceAsStream 在您的类路径中查找文件。 将您的 getResourceAsStream 更改为:

.getResourceAsStream("student.xml"));

确保指定路径

D:/workspace/RD_Axway/src/leo/test/digester/student/

在您的类路径中,然后重新编译并再次运行它。

【讨论】:

  • 你能显示你的类路径是什么吗?它在环境变量中,或者您在命令行中运行时定义它,例如 java -cp ...
  • 我正在使用 Eclipse IDE。感谢您的回复。
【解决方案4】:

getResourcegetResourceAsStream 都适用于相对于类路径的 URI。

您需要将路径修改为类似于

getResourceAsStream("/leo/test/digester/student/student.xml");

【讨论】:

  • 然后尝试不使用前面的'/'。如果 studen.xml 位于 leo/test/digester/student/ 目录下的运行时类路径中,它应该可以工作。
【解决方案5】:

获取加载我们类的对象的整个想法是能够独立于当前系统设置,从当前绝对路径获取资源。 您在getResourceAsStream 中得到空值,因为找不到文件。只需按照 Kayaman 的建议创建到 student.xml 的相对路径或类路径的绝对路径。

this.getClass()
.getClassLoader()
.getResourceAsStream("path/to/file/student.xml")

您通过classLoader.getResourceAsStream 使用与加载当前类相同的路径搜索顺序获取文件。请参阅 javadoc 获取 getResource:

java.lang.ClassLoader.getResource(字符串名称)

查找具有给定名称的资源。资源是一些数据 (图像、音频、文本等)可以通过类代码以某种方式访问 这与代码的位置无关。

资源名称是一个以“/”分隔的路径名,用于标识 资源。

此方法将首先在父类加载器中搜索 资源;如果 parent 为 null,则内置类加载器的路径 搜索到虚拟机。失败了,这个方法会 调用 findResource(String) 来查找资源。

由于这种概括,代码更具可移植性,无论您是在 Windows 还是 Linux 上,甚至在某些分布式系统上都没有关系。更多关于加载类和可能的来源:java.lang.ClassLoader

【讨论】:

    【解决方案6】:

    将文件放在当前文件夹或 uri 格式中。

    //placing the xml file in the current java file folder(which is not goog way of coding.
    
    DigesterTest ds = (DigesterTest) digest.parse(this.getClass().getResourceAsStream(
                 "test.xml"));
    DigesterTest ds = (DigesterTest) digest.parse("file://exact location of the file");
    //created a xml folder under java project 
    DigesterTest ds = (DigesterTest) digest.parse(new File("xml/test.xml"));
    

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2013-11-10
      • 2015-09-03
      • 1970-01-01
      • 2014-03-16
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多