我一直在尝试使用 java.lang 的 newInstace() 方法将我的一个类(使用反射加载)存储在一个对象中。它似乎在我创建 newInstace() 的方法中工作,但是在它之外,var 抛出一个空指针异常......对我来说真的没有意义,有人知道如何解决这个问题吗?
班级:
public class ScriptManager {
public static Class currentScript;
public static Object ScriptInstance;
public static int State;
// 0 = Not Running
// 1 = Running
// 2 = Paused
public static void runScript() {
try {
ScriptInstance = currentScript.newInstance();
currentScript.getMethod("run").invoke(ScriptInstance);
State = 1;
MainFrame.onPause();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void pauseScript() {
try {
currentScript.getMethod("pause").invoke(ScriptInstance);
State = 2;
MainFrame.onPause();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void stopScript() {
try {
currentScript.getMethod("stop").invoke(ScriptInstance);
State = 0;
MainFrame.onStop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误:
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at Bot.ScriptManager.pauseScript(ScriptManager.java:29)