如何解决"java.lang.InstantiationException"?

How to resolve a "java.lang.InstantiationException"?

我正在使用 SAX 在 XML 文件中进行解析,但是当我在 class 上调用 class 加载程序时,会抛出 java.lang.InstantiationException

我根据异常原因调试了这个,'Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.'

但是 location class 不是接口或抽象 class。我还检查了 class 是否在正确的包中,确实如此。

有人知道为什么在这种情况下抛出异常吗?

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

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...");
        }
    }

我的完整位置class:

http://hastebin.com/yofuyafipe.java

在 运行 时间内抛出的错误:

http://hastebin.com/jonozeyefe.avrasm

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

解决方案:

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

在这种情况下,第一个选项似乎更好...因为您在代码中的那个位置似乎没有所需的参数值。