java - Invocation target exception -
here code of program
import javax.swing.*; import java.awt.*; import java.awt.event.*; class jadapterokno extends windowadapter { public void windowclosing(windowevent e) { system.exit(0); } } class calcbutton extends jbutton implements actionlistener { kontroler k; calcbutton(kontroler k, string nazwa, int s) { super(nazwa); this.k = k; setfocusable(false); this.setfont(new font("tahoma", font.bold, s)); addactionlistener(this); } public void actionperformed(actionevent er) { k.dopisz(this); } } class infobutton extends jbutton implements actionlistener { kontroler k; infobutton(kontroler k, int s) { super("info"); this.k = k; setfocusable(false); addactionlistener(this); this.setfont(new font("tahoma", font.bold, s)); } public void actionperformed(actionevent err) { k.info(); } } class clearbutton extends jbutton implements actionlistener { kontroler k; clearbutton(kontroler k, int s) { super("c"); this.k = k; setfocusable(false); addactionlistener(this); this.setfont(new font("tahoma", font.bold, s)); } public void actionperformed(actionevent errr) { k.clear(); } } class wyswietlacz extends jtextpane implements keylistener { kontroler z; wyswietlacz(kontroler zz, int k) { super(); z = zz; setfocusable(true); seteditable(false); this.setfont(new font("tahoma", font.plain, k)); addkeylistener(this); } public void keypressed(keyevent evt) { char c = evt.getkeychar(); if (c == '0' || c == '1') { if (z.lorp) { if (z.lwyr.length() < 16) { z.lwyr.append(c); } } else { if (z.pwyr.length() < 16) { z.pwyr.append(c); } } } else { if (z.sign == '2' && c != '=' && z.lwyr.length() != 0) { z.lorp = false; z.sign = c; } if (z.sign != '2' && c == '=') { z.eval(true); } } z.eval(false); stringbuilder fin = new stringbuilder(z.lwyr); if (z.sign != '2') { fin.append(z.sign); fin.append(z.pwyr); } settext(fin.tostring()); } public void keyreleased(keyevent evt) { ; } public void keytyped(keyevent evt) { ; } } public class kontroler extends japplet { jmenubar menubar; /** * < zmienna menubar */ jmenu menu; /** * < zmienna menu */ jmenuitem menuitem; /** * < zmienna element menu */ stringbuilder lwyr; /** * < zmienna lewe wyrazenie obliczenia */ stringbuilder pwyr; /** * < zmienna prawe wyrazenie obliczenia */ final int modulo = (int) math.pow(2.0, 15.0); /** * < stala */ char sign; /** * < dzialanie */ boolean lorp; int x; int y; calcbutton inf; /** * < przycisk gui */ calcbutton zero; /** * < przycisk gui */ calcbutton one; /** * < przycisk gui */ calcbutton add; /** * < przycisk gui */ calcbutton sub; /** * < przycisk gui */ calcbutton div; /** * < przycisk gui */ calcbutton mlt; /** * < przycisk gui */ calcbutton modu; /** * < przycisk gui */ clearbutton blank; /** * < przycisk gui */ wyswietlacz wys; /** * < wyswietlacz */ jpanel przyciski; /** * < panel guzikow */ jpanel calosc; /** * < panel */ /** * metoda inicjalizujaca applet */ public void st() { menubar = new jmenubar(); menu = new jmenu("informacje"); menuitem = new jmenuitem("info"); menuitem.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { info(); } }); menubar.add(menu); menu.add(menuitem); setjmenubar(menubar); float xx = x / 240; float yy = y / 320; int avg = (int) ((xx + yy) / 2); system.out.println(modulo); setpreferredsize(new dimension(x, y)); inf = new calcbutton(this, "=", 15 * avg); 0 = new calcbutton(this, "0", 15 * avg); 1 = new calcbutton(this, "1", 15 * avg); add = new calcbutton(this, "+", 15 * avg); sub = new calcbutton(this, "-", 15 * avg); div = new calcbutton(this, "/", 15 * avg); mlt = new calcbutton(this, "*", 15 * avg); modu = new calcbutton(this, "%", 15 * avg); blank = new clearbutton(this, 20 * avg); wys = new wyswietlacz(this, 15 * avg); wys.setpreferredsize(new dimension(x, (int) (y / 4))); przyciski = new jpanel(); przyciski.setpreferredsize(new dimension(x, x)); przyciski.setlayout(new gridlayout(3, 3)); przyciski.add(zero); przyciski.add(one); przyciski.add(add); przyciski.add(sub); przyciski.add(mlt); przyciski.add(div); przyciski.add(modu); przyciski.add(inf); przyciski.add(blank); przyciski.setfocusable(false); calosc = new jpanel(); calosc.setlayout(new boxlayout(calosc, boxlayout.y_axis)); calosc.add(wys); calosc.add(przyciski); calosc.setpreferredsize(new dimension(x, y)); calosc.setfocusable(false); add(calosc); setfocusable(false); } public void init() { st(); } public void destroy() { ; } public void stop() { ; } kontroler() { this.lwyr = new stringbuilder(); this.pwyr = new stringbuilder(); this.sign = '2'; this.lorp = true; this.x = 240; this.y = 320; } kontroler(int x, int y) { if (x < 240 || y < 320) { joptionpane.showmessagedialog(null, "program nie dziala przy tak malej wielkosci okna"); system.exit(0); } this.lwyr = new stringbuilder(); this.pwyr = new stringbuilder(); this.sign = '2'; this.lorp = true; this.x = x; this.y = y; } void info() { joptionpane.showmessagedialog(null, "calc v1.1"); } void dopisz(calcbutton c) { string actual = c.gettext(); char ac = actual.charat(0); if (ac == '0' || ac == '1') { if (lorp) { if (lwyr.length() < 16) { lwyr.append(ac); } } else { if (pwyr.length() < 16) { pwyr.append(ac); } } } else { if (sign == '2' && ac != '=' && lwyr.length() != 0) { lorp = false; sign = ac; } if (sign != '2' && ac == '=') { eval(true); } } eval(false); stringbuilder fin = new stringbuilder(lwyr); if (sign != '2') { fin.append(sign); fin.append(pwyr); } c.k.wys.settext(fin.tostring()); } void clear() { this.lwyr = new stringbuilder(); this.pwyr = new stringbuilder(); this.wys.settext(""); } void eval(boolean t) { int l; int p; if (lwyr.length() == 0) { return; } if (t || (lwyr.length() == 16 && pwyr.length() == 16 && sign != '2')) { if (pwyr.length() == 0) { pwyr = lwyr; } l = integer.parseint(integer.valueof(this.lwyr.tostring(), 2).tostring()); p = integer.parseint(integer.valueof(this.pwyr.tostring(), 2).tostring()); if (l == 0 && p == 0) { lwyr = new stringbuilder(); pwyr = new stringbuilder(); sign = '2'; this.lorp = true; } switch (this.sign) { case '+': this.lwyr = new stringbuilder(integer.tostring((l + p) % modulo, 2)); break; case '-': this.lwyr = new stringbuilder(integer.tostring((l - p) % modulo, 2)); break; case '*': this.lwyr = new stringbuilder(integer.tostring((l * p) % modulo, 2)); break; case '/': this.lwyr = new stringbuilder(integer.tostring((l / p) % modulo, 2)); break; case '%': this.lwyr = new stringbuilder(integer.tostring((l % p) % modulo, 2)); break; } pwyr = new stringbuilder(); sign = '2'; this.lorp = true; } } /** * metoda main */ public static void main(string[] args) { jframe f = new jframe(); kontroler k; if (args.length == 0) { k = new kontroler(); f.add(k); k.init(); } else { k = new kontroler(integer.parseint(args[0]), integer.parseint(args[1])); f.add(k); k.init(); } f.pack(); f.setvisible(true); f.setresizable(false); } }
here full exception
basic: exception: java.lang.reflect.invocationtargetexception. java.lang.runtimeexception: java.lang.reflect.invocationtargetexception @ com.sun.deploy.uitoolkit.impl.awt.awtappletadapter.runonedtandwait(unknown source) @ com.sun.deploy.uitoolkit.impl.awt.awtappletadapter.instantiateapplet(unknown source) @ sun.plugin2.applet.plugin2manager.initappletadapter(unknown source) @ sun.plugin2.applet.plugin2manager$appletexecutionrunnable.run(unknown source) @ java.lang.thread.run(unknown source) caused by: java.lang.reflect.invocationtargetexception @ com.sun.deploy.uitoolkit.impl.awt.oldpluginawtutil.invokeandwait(unknown source) ... 5 more caused by: java.lang.runtimeexception: java.lang.illegalaccessexception: class com.sun.deploy.uitoolkit.impl.awt.awtappletadapter$1 can not access member of class kontroler modifiers "" @ com.sun.deploy.uitoolkit.impl.awt.awtappletadapter$1.run(unknown source) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source) caused by: java.lang.illegalaccessexception: class com.sun.deploy.uitoolkit.impl.awt.awtappletadapter$1 can not access member of class kontroler modifiers "" @ sun.reflect.reflection.ensurememberaccess(unknown source) @ java.lang.class.newinstance0(unknown source) @ java.lang.class.newinstance(unknown source) ... 20 more
when run in "normally", works; when try run applet in web browsers, recive invocation target exception? mistake? can't see exception starts because don't have details in java console
the root cause of exception, illegalaccessexception
, prompts quick review of what applets can , cannot do, applet doesn't appear violate sandbox.
running appletviewer
, shown here, shows following error, resolved making kontroler
constructors public
.
load: kontroler not public or has no public constructor. java.lang.illegalaccessexception: class sun.applet.appletpanel can not access member of class kontroler modifiers "" ...
in addition,
use key bindings ,
action
, illustrated here.consider alternate ways construct hybrid applet/application, shown here.
consider alternate ways deploy hybrid applet/application, shown here.
don't use
setpreferredsize()
.