[英]java.lang.IllegalArgumentException in AST method parse
[英]java.lang.IllegalArgumentException: InputStream to parse is null
请帮助我了解我的代码有什么问题。 我尝试通过此链接获取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 );
}
}
我相信这行是有问题的:
DigestStudents ds = (DigestStudents) digester.parse(this.getClass().getClassLoader().getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml"));
找不到资源,因此返回null。
ClassLoader.getResourceAsStream在您的类路径中查找文件。 将您的getResourceAsStream更改为:
.getResourceAsStream("student.xml"));
确保指定路径
D:/workspace/RD_Axway/src/leo/test/digester/student/
在您的类路径中,然后重新编译并再次运行。
getResource
和getResourceAsStream
都在相对于类路径的URI上工作。
您需要将路径修改为类似于
getResourceAsStream("/leo/test/digester/student/student.xml");
ClassLoader#getResourceAsStream()
能够找到相对于类路径“ 根 ”的文件。 您需要替换此行:
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"));
获取加载我们类的对象的整个想法是能够独立于当前系统设置和当前绝对路径独立获取资源。 由于找不到文件,因此在getResourceAsStream
得到null。 只需将指向student.xml
路径设为相对路径,或者将绝对路径设为Kayaman建议的classpath。
this.getClass()
.getClassLoader()
.getResourceAsStream("path/to/file/student.xml")
您可以通过classLoader.getResourceAsStream
获取文件,该文件具有用于加载当前类的相同路径搜索顺序。 请参阅javadoc获取getResource:
java.lang.ClassLoader.getResource(字符串名称)
查找具有给定名称的资源。 资源是某些数据(图像,音频,文本等),可由类代码以与代码位置无关的方式进行访问。
资源的名称是用“ /”分隔的路径名,用于标识资源。
此方法将首先在父类加载器中搜索资源; 如果父级为null,则搜索虚拟机内置的类加载器的路径。 失败的话,此方法将调用findResource(String)来查找资源。
归功于这种通用性,无论您是在Windows还是Linux上,甚至在某些分布式系统上,代码都更加可移植。 有关加载类和可能的源的更多信息: java.lang.ClassLoader
将文件放置为当前文件夹或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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.