摘要器解析器错误 java.lang.NoSuchMethodException: Employee.<init>() [英] digester parser error java.lang.NoSuchMethodException: Employee.<init>()

查看:38
本文介绍了摘要器解析器错误 java.lang.NoSuchMethodException: Employee.<init>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 digester 解析 xml.

我的 XML

<员工><Id>1</Id><FirstName>Charles</FirstName><LastName>Madigen</LastName><位置>路易斯安那</位置><技能>会计师</技能></员工></root>

我的员工类

公共类员工{私人国际empId;私人字符串 fName;私人字符串 lName;私人字符串位置;私人字符串技能;公共员工(int empId,字符串 fName,字符串 lName,字符串位置,字符串技能) {this.empId = empId;this.fName = fName;this.lName = lName;this.location = 位置;this.skill = 技能;}@覆盖公共字符串 toString() {return "Employee [empId=" + empId + ", fName=" + fName + ", lName="+ lName + ", location=" + location + ", Skill=" + Skill + "]";}公共无效 setEmpId(int empId) {this.empId = empId;}public void setfName(String fName) {this.fName = fName;}public void setlName(String lName) {this.lName = lName;}公共无效设置位置(字符串位置){this.location = 位置;}公共无效setSkill(字符串技能){this.skill = 技能;}}

和我的读者班

公共类 CSVtoXMLTransformer {public static void main(String[] args) 抛出 IOException {CSVtoXMLTransformer cx=new CSVtoXMLTransformer();//cx.transfromer();cx.validator();}无效验证器()抛出 IOException{String itemTag = "root/Employee";消化器消化器 = new Digester();摘要器.setValidating(假);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" );员工 em;尝试 {emp = (Employee)digester.parse( inputFile );System.out.println(emp);} catch (SAXException e) {//TODO 自动生成的 catch 块e.printStackTrace();}}}

但是在运行时我收到此错误,有人可以帮助我吗

org.xml.sax.SAXParseException;systemId: 文件:/D:/workspace/poc2/generatedEmployee.xml;行号:2;列数:11;第 2 行字符 11 处的错误:assignment3.Employee在 org.apache.commons.digester.Digester.createSAXException(Digester.java:3363).......引起:java.lang.NoSuchMethodException: assignment3.Employee.()在 java.lang.Class.getConstructor0(来源不明)……还有 17 个……

解决方案

Caused by: java.lang.NoSuchMethodException: assignment3.Employee.()在 java.lang.Class.getConstructor0(来源不明)

在您编译的代码中找不到带有签名的方法 assignment3.Employee.<init>() 这就是 JVM 引发 java.lang.NoSuchMethodException 异常的原因.>

在您的类中,您创建了参数化构造函数,当您创建参数化构造函数时,编译器不会创建默认构造函数,因此您也必须实现默认构造函数.

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.....

解决方案

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.

这篇关于摘要器解析器错误 java.lang.NoSuchMethodException: Employee.&lt;init&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆