【问题标题】:How to resolve a "java.lang.InstantiationException"?如何解决“java.lang.InstantiationException”?
【发布时间】:2015-03-22 01:12:46
【问题描述】:

我正在使用 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

【问题讨论】:

  • 您可能需要在 Location 类上使用不带参数的构造函数。
  • 不要对非代码文本使用代码格式,也不要发布链接到构成您问题一部分的内容。在这里发布。
  • 下次……包括完整的堆栈跟踪。
  • 而且没有屏幕截图。 “Location 元素的 XML 结构”与它有什么关系是一个谜,就像您提供的图片与 XML、问题或其他任何东西有什么关系一样。

标签: java xml classloader sax instantiationexception


【解决方案1】:

您的Location 类没有无参数构造函数。 (它有两个带有声明参数的构造函数……所以没有默认的无参数构造函数。)

解决方案:

  • 添加无参数构造函数。
  • 反射性地在位置Class 对象上查找Constructor 对象之一,并使用Constructor.newInstance(...) 使用提供实际构造函数参数值的参数调用它。

看起来第一个选项在这种情况下更好……因为看起来你在代码中没有所需的参数值。

【讨论】:

  • 是的,这就是问题所在,我没有默认构造函数。以下解决了它:public Location() { super(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
  • 2018-06-01
相关资源
最近更新 更多