我發佈了這個this問題,這基本上等於嘗試將Strings映射到Java中的函數。我試圖通過創建一個HashMap <String, Method>
來解決它,但是每次我嘗試運行Method.invoke(this,args),其中args是對應於Method參數的對象數組時,我得到一個NullPointerException
。我在我的HashMap
中聲明方法如下:mypackage.MyClass.class.getMethod ("methodName", args)
,其中args
是對應於參數methodName
的Class
對象的數組。我一直在爲此工作了幾個小時,我開始非常沮喪。如果有人有任何建議,我會非常感激!Java:Method.invoke(this,args)NullPointerException
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)
請給我們看一些代碼和堆棧跟蹤。真的很難說出發生了什麼事。 – 2012-08-14 21:15:00
謝謝你的幫助,每個人......很明顯,我在StackOverflow格式化方面遇到了一些麻煩。 – 2012-08-14 21:25:36
是invoke()中的「this」調用正確類型的對象嗎?在你的情況下,它必須是一個MyClass對象 – jrochette 2012-08-14 21:33:05