我发布了这个问题,基本上相当于尝试将字符串映射到 Java 中的函数。我试图通过创建HashMap <String, Method>
一个NullPointerException
. 我在 my 中声明方法HashMap
如下:mypackage.MyClass.class.getMethod ("methodName", args)
,其中是与 的参数对应args
的对象数组。我已经为此工作了几个小时,我开始感到非常沮丧。如果有人有任何建议,我将不胜感激!Class
methodName
HashMap <String, Method> myMethods = new HashMap <String, Method> ();
Class [] args = {param1.class, param2.class, param3.class};
myMethods.put ("aKey", mypackage.MyClass.class.getMethod ("aMethod", args));
Method thisTest = myTests.get (thisKey);
Object [] args1 = {param1, param2, param3};
String thisResult = (String) thisTest.invoke (this, args1); // NPE occurs in this line
Aug 14, 2012 10:37:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at mypackage.MyClass.myMethod (MyClass.java:61)
at org.apache.jsp.myMethod_jsp._jspService(getZabbix_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)