Search code examples
javaxmlapache-commons-digester

java.lang.IllegalArgumentException: InputStream to parse is null


Please help me to know what is wrong with my code.I tried from this link for Sample. OS : Windows 7

Code 1 :

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

Exception 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)

Code 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);

Exception 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 );
    }
}

Solution

  • ClassLoader#getResourceAsStream() is able to locate files relative to the "root" of the classpath. You need to replace this line:

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

    With

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