gpt4 book ai didi

Java:NoSuchMethodException

转载 作者:行者123 更新时间:2023-12-02 04:06:03 27 4
gpt4 key购买 nike

对于我的程序,目标是使用一堆接口(interface)并创建一个程序来进行测试并允许用户进行测试。我遇到的具体问题是 IQuestionFactory 接口(interface),它有助于在 TestMaker 类中提出问题。我正在使用我的教授提供的类加载器,它使用我需要加载的类的名称作为参数。我不明白为什么会收到错误。

错误

java.lang.NoSuchMethodException: test.api.IQuestionFactory.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at test.util.MyClassLoader.createInstance(MyClassLoader.java:37)
at test.maker.TestMaker.main(TestMaker.java:13)
Exception in thread "main" java.lang.NullPointerException
at test.maker.TestMaker.main(TestMaker.java:14)

类加载器

package test.util;

import java.lang.reflect.Constructor;

public class MyClassLoader {

//instead of using the constructor, we provide a single instance of MyClassLoader
public static MyClassLoader instance = new MyClassLoader();

//by making the constructor private, we ensure that it can't be called.
private MyClassLoader() {

}

/**
* Load class of the given className and create an object of that class.
* @param className the name of the class including its package. e.g. test.impl.QuestionFactory
* @return return the object created.
*/
public Object createInstance(String className) {
try {
ClassLoader classLoader = this.getClass().getClassLoader();
Class loadedMyClass = classLoader.loadClass(className);
Constructor constructor = loadedMyClass.getConstructor();
Object myClassObject = constructor.newInstance();
return myClassObject;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

TestMaker 类

package test.maker;

import test.api.IQuestion;
import test.api.IQuestionFactory;
import test.api.ITestSet;
import test.util.MyClassLoader;

public class TestMaker {
public static void main (String[] args){

MyClassLoader cl = MyClassLoader.instance;

IQuestionFactory factory = (IQuestionFactory) cl.createInstance("test.api.IQuestionFactory");
ITestSet testset = factory.makeEmptyTestSet();

String question = "Which of the following people was not a US president?";
String[] choices = {"George Washington", "Benjamin Franklin", "Andrew Jackson", "Mr. Rodgers"};
int answer = 1;

IQuestion q = factory.makeMultipleChoice(question, choices, answer);
testset.add(q);

factory.save(testset,"Questions");

}
}

IQuestionFactory接口(interface)

package test.api;

import java.io.IOException;


public interface IQuestionFactory {
/**
* @param question The question
* @param choices The choices as an array
* @param answer The index to the answer in the choices array
* @return An instance of a multiple choice question.
*/
public IQuestion makeMultipleChoice(String question, String[] choices, int answer);


/**
* @param question The question.
* @param answer The answer
* @return an instance of a true-false question
*/
public IQuestion makeTrueFalse(String question, boolean answer);

/**
* @param question The question, including the blanks
* @param answers Array of answers to the blanks
* @return an instance of a fill-in-the-blank question
*/
public IQuestion makeFillInBlank(String question, String [] answers);

/**
* @param question The question.
* @param keywords The answers as a list of key words.
* @return an instance of a short answer question.
*/
public IQuestion makeShortAnswer(String question, String[] keywords);

/**
* @param filename The file containing the test set.
* @return A Test set
* @throws IOException if can't load the file.
*/
public ITestSet load(String filename);


/**
* @param testSet The test set to be stored.
* @param filename The filename to be used.
* @return true if save is successful
* @throws IOException if unable to save the test set.
*/
public boolean save(ITestSet testSet, String filename);


/**
* Create an empty test set.
*
* @return an empty test set.
*/
public ITestSet makeEmptyTestSet();


}

最佳答案

您正在尝试创建接口(interface)的实例,这没有任何意义。您只能创建类的实例。您需要使用另一个类来实现该接口(interface),并使用该类作为 cl.createInstance 的参数。

编辑:虽然我真的不知道你为什么在这里使用反射。你可能应该做这样的事情:

IQuestionFactory factory = new MyQuestionFactory()

其中 MyQuestionFactory 实现 IQuestionFactory。这样效率更高,然后编译器会发现你的错误。您现在正在做的是:

IQuestionFactory factory = new IQuestionFactory()

编译器会捕捉到这一点。

关于Java:NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258595/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com