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

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

问题描述

我有一个关于一个特别烦的错误,我一直无法找出问题,更谈不上解决。任何时候,我尝试在我的网站上运行Java applet(applet或JApplet的),我得到这个错误作为一个弹出窗口:

  java.lang.reflect.InvocationTargetException

没有堆栈跟踪,没有行号,只是错误信息。所以我一派四处寻找别人的解决方法(或理想的实际修复),但一直没能找到很多。我用尽code的几个变化(有时一个JAR文件,有时没有,有时一个班,有时没有,有时在使用匹配的目录结构,时有时无包装等包装),但可以'不像是会突破这个讨厌的小儿子的一错误。 :)

有关一个具体的例子,这是我最近一次尝试;首先在Java code:

 包cmtoolbox;公共类CMToolbox {
    公共静态无效的主要(字串[] args){
        MainApplet一个=新MainApplet();
    }
}

类它设置:

 包cmtoolbox;进口javax.swing.JApplet中;
进口javax.swing.JButton中;公共类MainApplet扩展JApplet的{
    公共MainApplet(){
        JApplet的主要=新JApplet的();
        main.setSize(800600);
        JButton的TEST1 =的新的JButton();
        test1.setText(测试);
        main.add(TEST1);
    }
}

我的HTML code:

< HTML和GT;
< HEAD>
  <标题>与Java小程序&LT实验; /标题>
< /头>
<身体GT;
  < P><小程序code =CMToolbox.classWIDTH =800WIDTH =600>
    我希望。 :)
  < /小程序>< / P>
< /身体GT;
< / HTML>

我想这也许是因为网络本身可以有这么多的变数(操作系统,浏览器类型等),也有一些是内部/系统级造成这个......但我确实有上安装了JRE和JDK我电脑,所以我真的不知道为什么...无论如何,我敢肯定,我不是第一个家伙打这个路障,但它难住我了,所以我倒是AP preciate可能提供的任何信息就此主题而言。另外,如果你知道任何好的的Java Web教程绝对菜鸟,这将是巨大的为好。 :)


解决方案

的InvocationTargetException 1 被抛出,因为HTML是调用(试图加载)的东西是不会的小程序。将其更改为:

< P><小程序code =MainAppletWIDTH =800WIDTH =600 >
  < /小程序>< / P>

此外,如斯蒂芬ç的答复中提到。从构造移动的东西到的init()方法。


  

      
  1. 的InvocationTargetException 是一个检查异常包装异常由调用方法或构造函数抛出。

  2.   

确保 Java控制台配置,以示对小程序和放大器; JWS应用程序。如果在默认级别无输出,提高并重试。


虽然我在这里:为什么code的小程序?如果是由于天赋。由教师,请他们参考<一个href=\"http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/\"相对=nofollow>为什么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天全站免登陆