如何解决“ java.lang.InstantiationException”?

问题描述 投票:5回答:2

我正在使用SAX解析XML文件,但是当我在类上调用类加载器时,会抛出java.lang.InstantiationException

我通过例外原因进行调试,'当应用程序尝试使用Class类中的newInstance方法创建类的实例时抛出,但是指定的类对象由于是接口或抽象而无法实例化课。'

但是location类不是接口或抽象类。我还检查了该类是否在正确的包中,并且它是正确的。

有人知道为什么在这种情况下会引发异常吗?

该异常在Parser类的startElement中的第一个println之后抛出:

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        if (qName.equals("location")){
            location = true;

            System.out.println("Found a location...");
            //exception thrown after this statement as shown
            //in the error output below
            try {
                //Read in the values for the attributes of the element <location>
                int locationID = Integer.parseInt(atts.getValue("id"));
                String locationName = atts.getValue("name");

                //Generate a new instance of Location on-the-fly using reflection. The statement Class.forName("gmit.Location").newInstance(); invokes the 
                //Java Class Loader and the calls the null (default) constructor of Location.
                Location loc = (Location) Class.forName("gmit.Location").newInstance();
                loc.setId(locationID); //Now configure the Location object with an ID, Name, Description etc...
                loc.setName(locationName);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else if (qName.equals("description")){
            description = true;
            System.out.println("Found a description. You should tie this to the last location you encountered...");
        }else if (qName.equals("exits")){
            exits = true;
            System.out.println("Found an exit. You should tie this to the last location you encountered...");
        }else if (qName.equals("item")){
            item = true;
            System.out.println("Found an item. You should tie this to the last game-character you encountered if the boolean gameCharacter flag is true...");
        }else if (qName.equals("game-character")){
            gameCharacter = true;
            System.out.println("Found a game character...");
        }else if (qName.equals("search-algorithm")){
            searchAlgorithm = true;
            System.out.println("Found a search algo. You should tie this to the last game-character you encountered if the boolean gameCharacter flag is true...");
        }
    }

我完整的位置信息类别:

http://hastebin.com/yofuyafipe.java

运行时抛出的错误:

http://hastebin.com/jonozeyefe.avrasm

java xml classloader sax instantiationexception
2个回答
8
投票

您的Location类没有无参数的构造函数。 (它有两个带有声明参数的构造函数...因此没有默认的无参数构造函数。)

解决方案:

  • 添加一个无参数的构造函数。
  • 以反射方式在Location Constructor对象上查找Class对象之一,并使用Constructor.newInstance(...)调用它并提供给出实际构造函数参数值的参数。

在这种情况下,第一个选择似乎是更好的选择……'因为看起来您在代码中没有必要的参数值。


0
投票

感谢Stephen C.

Caused by: java.lang.reflect.UndeclaredThrowableException: null
    at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:120) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:250) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:196) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    ... 50 common frames omitted
Caused by: java.lang.InstantiationException: com.example.batchprocessing.Price
    at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_131]
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:247) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    ... 53 common frames omitted

向价格类添加public Price(){};,已解决问题(顺便说一句,这是一个基于springbatch的应用程序。)>]

public class Price {

    private String x;
    private String y;

    public Price(){};

    public price(String x, String y) { this.x = x ; this.y = y; };

~
~
~

}
© www.soinside.com 2019 - 2024. All rights reserved.