java.lang.IllegalArgumentException:要解析的InputStream为null

时间:2013-09-13 07:25:47

标签: java xml apache-commons-digester

请帮助我了解我的代码有什么问题。I tried from this link for Sample.操作系统: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 );
    }
}

6 个答案:

答案 0 :(得分:1)

我认为这条线存在问题:

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

无法找到资源,因此返回null。

答案 1 :(得分:1)

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

.getResourceAsStream("student.xml"));

确保指定路径

D:/workspace/RD_Axway/src/leo/test/digester/student/
在你的类路径中

然后重新编译并再次运行它。

答案 2 :(得分:1)

getResourcegetResourceAsStream都处理相对于类路径的URI。

您需要将路径修改为

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

答案 3 :(得分: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"));

答案 4 :(得分:1)

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

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

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

  

java.lang.ClassLoader.getResource(String name)

     

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

     

资源的名称是'/' - 标识的分隔路径名   资源。

     

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

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

答案 5 :(得分:0)

将文件放在当前文件夹或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"));