我是反思的新手。我面临一些错误。请帮忙。下面是我的代码:
EmployeeClass.java:
public class EmployeeClass {
private String empID;
private String empName;
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public EmployeeClass(String empID, String empName) {
this.empID = empID;
this.empName = empName;
}
public String getAllDetails() {
return empID + " " + empName;
}
}
反射类.java:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionClass {
public static void main(String[] args) {
EmployeeClass emp = new EmployeeClass("1", "Emp1");
Method method = null;
try {
method = emp.getClass().getMethod("getAllDetails", null);
System.out.println(method.invoke(null, null));
} catch (NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
System.out.println(e.getMessage());
}
}
}
运行 ReflectionClass.java 时,出现以下错误:
线程“主”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 myprgs.programs.ReflectionClass.main(ReflectionClass.java:14)