Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

xml - digester parser error java.lang.NoSuchMethodException: Employee.<init>()

I am trying to parse an xml using digester .

My XML

<root>
<Employee>
    <Id>1</Id>
    <FirstName>Charles</FirstName>
    <LastName>Madigen</LastName>
    <Location>Louisiana</Location>
    <Skill>Accountant</Skill>
</Employee>
</root>

My Employee class

public class Employee { 
    private int empId;
    private String fName;
    private String lName;
    private String location;
    private String skill;   
    public Employee(int empId, String fName, String lName, String location,
            String skill) {
        this.empId = empId;
        this.fName = fName;
        this.lName = lName;
        this.location = location;
        this.skill = skill;
    }
    @Override
    public String toString() {
        return "Employee [empId=" + empId + ", fName=" + fName + ", lName="
                + lName + ", location=" + location + ", skill=" + skill + "]";
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public void setLocation(String location) {
        this.location = location;
    }
    public void setSkill(String skill) {
        this.skill = skill;
    }

}

and my reader class

public class CSVtoXMLTransformer {

    public static void main(String[] args) throws IOException  {        
        CSVtoXMLTransformer cx=new CSVtoXMLTransformer();
        //cx.transfromer();
        cx.validator();
    }                               

    void validator() throws IOException{                        
        String itemTag = "root/Employee";
        Digester digester = new Digester();
        digester.setValidating(false);
        digester.addObjectCreate(itemTag, "assignment3.Employee");
        digester.addCallMethod(itemTag + "/Id", "setEmpId", 0);
        digester.addCallMethod(itemTag + "/FirstName", "setfName", 0);
        digester.addCallMethod(itemTag + "/LastName", "setlName", 0);
        digester.addCallMethod(itemTag + "/Location", "setLocation", 0);
        digester.addCallMethod(itemTag + "/Skill", "setSkill", 0);   

          File inputFile = new File( "generatedEmployee.xml" );
          Employee emp;
        try {
            emp = (Employee)digester.parse( inputFile );
             System.out.println(emp);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }                               
    }        
}

But while running I am getting this error, Can any one help me on this

org.xml.sax.SAXParseException; systemId: file:/D:/workspace/poc2/generatedEmployee.xml; lineNumber: 2; columnNumber: 11; Error at line 2 char 11: assignment3.Employee
    at org.apache.commons.digester.Digester.createSAXException(Digester.java:3363).......
Caused by: java.lang.NoSuchMethodException: assignment3.Employee.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    ... 17 more.....
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Caused by: java.lang.NoSuchMethodException: assignment3.Employee.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)

Method with signature assignment3.Employee.<init>() is not found in your compiled code that's why JVM raised java.lang.NoSuchMethodException Exception.

In your class you have created parametrized constructor, when you create parameterized constructor compiler will not create default constructor so you have to implement default constructor as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...