Wie zu beheben eine “java.lang.InstantiationException"?

Ich bin Parsen einer XML-Datei mit SAX, aber wenn ich rufen Sie den class loader in der Klasse, ein java.lang.InstantiationException geworfen wird.

Ich gedebuggt dies der Grund für die Ausnahme " Geworfen, wenn eine Anwendung versucht, eine Instanz einer Klasse mit der newInstance-Methode der Klasse Class, aber das angegebene Objekt der Klasse nicht instanziiert werden kann, weil es eine Schnittstelle oder eine abstrakte Klasse.'

Aber die location - Klasse ist nicht eine Schnittstelle oder eine abstrakte Klasse. Ich habe auch überprüft, dass die Klasse im richtigen Paket und es ist.

Hat jemand eine Idee, warum die exception geworfen wird, in diesem Fall?

Ausnahme wird geworfen, gleich nach dem ersten println in der startElement der Parser-Klasse:

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

Meine komplette Platz Klasse:

http://hastebin.com/yofuyafipe.java

Den Fehler ausgelöst wird, während der Laufzeit:

http://hastebin.com/jonozeyefe.avrasm

  • Wahrscheinlich brauchen Sie einen Konstruktor auf den Speicherort der Klasse, der hat keine Argumente.
  • Benutze nicht die code-Formatierung für text, der nicht code, und postet keine links zu Materie, die ein Bestandteil Ihrer Frage. Poste es hier.
  • Und das nächste mal ... umfassen den gesamten stacktrace.
  • Und keine screenshots. Was die 'XML-Struktur der Location element' hat zu tun mit ist es ein Rätsel, wie das, was das Bild, das Sie zur Verfügung hatte zu tun mit XML, oder die Frage, oder eigentlich alles.
InformationsquelleAutor Brian J | 2015-03-22
Schreibe einen Kommentar