I posted this this question, which basically amounts to an attempt to map Strings to functions in Java. I am trying to solve it by creating a HashMap <String, Method>
, but every time I try to run Method.invoke (this, args), where args is an array of Objects corresponding to the parameters of Method, I get a NullPointerException
. I declare the Methods in my HashMap
as follows: mypackage.MyClass.class.getMethod ("methodName", args)
, where args
is an array of Class
object corresponding to the parameters of methodName
. I've been working on this for a couple of hours and I'm starting to get really frustrated. If anyone has any suggestions I would really appreciate it!
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)