Java:Method.invoke (this, args) NullPointerException [英] Java: Method.invoke (this, args) NullPointerException
问题描述
我发布了这个这个 问题,这基本上相当于尝试将字符串映射到 Java 中的函数.我试图通过创建一个 HashMap
来解决它,但每次我尝试运行 Method.invoke (this, args),其中 args 是对应于Method 的参数,我得到一个 NullPointerException
.我在 HashMap
中声明方法如下:mypackage.MyClass.class.getMethod ("methodName", args)
,其中 args
是一个与methodName
的参数对应的Class
对象数组.我已经为此工作了几个小时,我开始感到非常沮丧.如果有人有任何建议,我将不胜感激!
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)
推荐答案
虽然没有显示myTests
Map的填充,但显然没有包含测试方法thisTest
导致 NPE.
Although the population of myTests
Map is not shown, it obviously doesn't contain the test method thisTest
resulting in an NPE.
这里要做的第一件事是检查这个 Map 的内容以确保它包含请求的键.
The first thing to do here is to inspect the contents of this Map to ensure that it contains the requested key.
此外,值得拥有一个保护条款 在 thisTest
上尝试调用该方法以防止它爆炸.
Also, it's worth having a guard clause on thisTest
before you attempt to invoke the method to prevent it blowing up.
这篇关于Java:Method.invoke (this, args) NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!