Java:调用目标异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6388051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: InvocationTargetException
提问by Amir Rachum
I am dynamically creating classes in Java and trying to invoke methods in them, however, sometimes I get a java.lang.reflect.InvocationTargetException
.
我在 Java 中动态创建类并尝试调用其中的方法,但是,有时我会得到一个java.lang.reflect.InvocationTargetException
.
PageGenerator1.java (dynamically created)
PageGenerator1.java(动态创建)
import java.io.PrintStream;
import java.util.Map;
public class PageGenerator1 implements DynamicPageGenerator {
public PageGenerator1() {
}
@Override
public void generate(PrintStream out, Map<String,String> params, Session session) {
out.print("<html>\r\n<body>\r\n");
if (session.get("counter") == null) {
session.set("counter", 2);
out.println("<h1>Hi "+params.get("name")+" this is your first visit</h1>");
} else {
out.println("<h1>This is your "+session.get("counter")+" visit</h1>");
session.set("counter", 1+((Integer)session.get("counter")));
}
out.print("\r\n</body>\r\n</html>");
}
}
I am trying to invoke it like so:
我试图像这样调用它:
logger.info(
"Attempting to invoke the method " + generateMethod + " with an instance of " + generatedClassName + "with the following parameters:\n" +
"\tparams: " + params + "\n" +
"\tcookieSession: " + cookiesSession
);
generateMethod.invoke(Class.forName(generatedClassName).newInstance(), ps, params, cookiesSession);
and this is the log entry I get:
这是我得到的日志条目:
INFO: Attempting to invoke the method
public void cs236369.webserver.requesthandlers.tsp.PageGenerator1.generate(java.io.PrintStream,java.util.Map,cs236369.webserver.requesthandlers.tsp.Session)
with an instance ofcs236369.webserver.requesthandlers.tsp.PageGenerator1
with the following parameters:
params:{name=Amir}
cookieSession:{counter=5}
信息:尝试
public void cs236369.webserver.requesthandlers.tsp.PageGenerator1.generate(java.io.PrintStream,java.util.Map,cs236369.webserver.requesthandlers.tsp.Session)
使用具有cs236369.webserver.requesthandlers.tsp.PageGenerator1
以下参数的实例调用方法:
params:{name=Amir}
cookieSession:{counter=5}
The exception I'm getting doesn't have a message, and I'm not experienced with reflection, etc. so I'm not sure what the error means. Can you help explain what am I doing wrong?
我得到的异常没有消息,而且我没有反射等经验,所以我不确定错误意味着什么。你能帮忙解释一下我做错了什么吗?
采纳答案by Kal
InovcationTargetException means that the method that you invoked threw an exception. To figure out what the problem is with your method itself, wrap the invoke method call around a try-catch block and log invocationTargetException.getTargetException()
.
InovcationTargetException 表示您调用的方法引发了异常。要找出您的方法本身的问题,请将 invoke 方法调用包装在 try-catch 块和 log 周围invocationTargetException.getTargetException()
。
I can see several places in your generateMethod that you could have errors. Session could be null, session.getCounter() is being cast to Integer -- there could be a classcastexception there.
我可以在您的 generateMethod 中看到几个可能有错误的地方。Session 可能为 null, session.getCounter() 被转换为 Integer —— 那里可能存在 classcastexception。
回答by Basanth Roy
Put try catch blocks in both your invocation code as well as the generate blocks. Additionally, you can also step through the methods in a debugger.
将 try catch 块放在调用代码和 generate 块中。此外,您还可以单步调试调试器中的方法。
回答by Deepak Vajpayee
It may be because of wrong parameters. First, check your parameters. Use e.getCause().getCause() to get actual cause behind this.
可能是因为参数错误。首先,检查您的参数。使用 e.getCause().getCause() 获取背后的实际原因。