如何修复/解决 java.lang.reflect.InvocationTargetException [英] How to fix/workaround java.lang.reflect.InvocationTargetException

查看:215
本文介绍了如何修复/解决 java.lang.reflect.InvocationTargetException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于一个特别烦人的错误的问题,我一直无法弄清楚,更不用说克服了.每当我尝试在我的网站上运行 Java 小程序(Applet 或 JApplet)时,我都会以弹出窗口的形式收到此错误:

 java.lang.reflect.InvocationTargetException

没有堆栈跟踪,没有行号,只有错误信息.所以我在谷歌上四处寻找其他人的解决方法(或理想情况下的实际修复),但没有找到太多.我已经尝试了我的代码的几种变体(有时使用 JAR 文件,有时不使用,有时使用单个类,有时不使用,有时在使用匹配目录结构的包中,有时没有包等)但似乎无法摆脱这个讨厌的小虫子.:)

举个具体的例子,这是我最近的尝试;首先是Java代码:

包cmtoolbox;公共类 CMToolbox {公共静态无效主(字符串 [] args){MainApplet a = new MainApplet();}}

它设置的类:

包cmtoolbox;导入 javax.swing.JApplet;导入 javax.swing.JButton;公共类 MainApplet 扩展 JApplet {公共主小程序(){JApplet main = new JApplet();main.setSize(800,600);JButton test1 = new JButton();test1.setText("测试");main.add(test1);}}

我的 HTML 代码:

<头><标题>试验 Java 小程序 </title><身体><p><applet 代码="CMToolbox.class" width="800" width="600">我希望.:)</applet></p>

我想可能是因为网络本身可以有太多的变量(操作系统、浏览器类型等),有一些内部/系统级别的原因导致了这种情况……但我确实在我的机器上安装了 JRE 和 JDK计算机,所以我真的不明白为什么......无论如何,我肯定我不是第一个遇到这个障碍的人,但这让我很难过,所以我很感激任何可能有关该主题的信息.此外,如果您知道任何针对绝对新手的优秀 Java 网络教程,那也很棒.:)

解决方案

InvocationTargetException1 被抛出,因为 HTML 正在调用(试图加载) 不是 小程序.改为:

 

</applet></p>

此外,正如Stephen C 的回答 中所述.将构造函数中的内容移动到 init() 方法中.

<块引用>

  1. InvocationTargetException 是经过检查的异常,它包装由调用的方法或构造函数抛出的异常.

确保 Java 控制台 配置为显示小程序和;JWS 应用程序.如果默认级别没有输出,请提高并重试.

<小时>

当我在这里时:为什么要编写小程序?如果是由于规格.老师请参考为什么 CS 教师应该停止教授 Java 小程序.

I've got a question about a particularly annoying error that I haven't been able to figure out, much less overcome. Any time I try to run a Java applet (Applet or JApplet) on my website, I get this error as a pop-up:

 java.lang.reflect.InvocationTargetException

No stack trace, no line number, just the error message. So I've Googled around looking for anyone else's workarounds (or ideally actual fixes) but haven't been able to find much. I've tried several variations of my code (sometimes with a JAR file, sometimes not, sometimes a single class, sometimes not, sometimes in a package using a matching directory structure, sometimes no package, etc.) but can't seem to get past this nasty little son-of-a-bug. :)

For a specific example, here's my most recent attempt; first the Java code:

package cmtoolbox;

public class CMToolbox {
    public static void main(String[] args) {
        MainApplet a = new MainApplet();
    }
}

The class it sets up:

package cmtoolbox;

import javax.swing.JApplet;
import javax.swing.JButton;

public class MainApplet extends JApplet {
    public MainApplet() {
        JApplet main = new JApplet();
        main.setSize(800,600);
        JButton test1 = new JButton();
        test1.setText("Test");
        main.add(test1);
    }
}

My HTML code:

<html>
<head>
  <title> Experimenting with Java applets </title>
</head>
<body>
  <p><applet code="CMToolbox.class" width="800" width="600">
    I wish. :)
  </applet></p>
</body>
</html>

I suppose that maybe because the web itself can have so many variables (operating systems, browser types, etc.) there is something internal/system-level causing this... but I do have the JRE and JDK installed on my computer so I don't really get why... Anyway, I'm sure I'm not the first guy to hit this roadblock, but it's got me stumped so I'd appreciate any info that may be available on the subject. Also if you know of any good Java web tutorials for absolute noobs that would be great as well. :)

解决方案

InvocationTargetException1 is thrown because the HTML is calling (trying to load) something that is not an applet. Change it to:

  <p><applet code="MainApplet" width="800" width="600">
  </applet></p>

Also, as mentioned in the answer of Stephen C. Move the stuff from the constructor into the init() method.

  1. InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.


While I'm here: Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.

这篇关于如何修复/解决 java.lang.reflect.InvocationTargetException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆