5

SAX を使用して XML ファイルを解析していますが、クラスでクラス ローダーを呼び出すと、ajava.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/jonozeeyefe.avrasm

4

1 に答える 1

12

クラスに引数なしのコンストラクターがありませLocationん。(宣言された引数を持つ2つのコンストラクターがあります...したがって、デフォルトの引数なしのコンストラクターはありません。)

ソリューション:

  • 引数なしのコンストラクターを追加します。
  • ConstructorLocationオブジェクトのオブジェクトの 1 つを反射的に検索し、実際のコンストラクター引数値を与える引数をClass使用して呼び出します。Constructor.newInstance(...)

このコンテキストでは、最初のオプションの方が優れているように見えます...コードのその時点で必要な引数値がないように見えます.

于 2015-03-22T01:19:27.933 に答える