What's wrong with this Java Applet code? -
i'm trying run code in java applet via appletviewer in fedora 18. error
java.lang.nullpointerexception @ main.init(main.java:42) @ sun.applet.appletpanel.run(appletpanel.java:436) @ java.lang.thread.run(thread.java:722)
as per code 42nd line in code bmul.setbounds(100, 280, 50, 50);
isn't wrong as-far-as know. after searching on google, found
nullpointerexception runtime exception thrown jvm when application code, other referenced api(s) or middleware (weblogic, was, jboss...) encounters next conditions:
attempting invoke instance method of null object attempting access or modify particular field of null object attempting obtain length of such null object arrayi've tried hard failed in making work. please help me. here provide main.java file's code.
import java.awt.*; import java.applet.*; import java.awt.event.*; public class main extends applet implements actionlistener { button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9; button badd, bsub, bmul, bdiv, bequ, bcls, bdec, bsqrt, bsin, bcos, btan; textfield tf1; int add, sub, mul, div, temp = 0; double sqr, s, c, ta; string t = ""; public void init() { setlayout(null); setfont(new font("cantarell", font.bold, 12)); b0 = new button("0"); b1 = new button("1"); b2 = new button("2"); b3 = new button("3"); b4 = new button("4"); b5 = new button("5"); b6 = new button("6"); b7 = new button("7"); b8 = new button("8"); b9 = new button("9"); badd = new button("+"); bsub = new button("-"); bsub = new button("*"); bdiv = new button("/"); bequ = new button("="); bsqrt = new button("sqrt"); bsin = new button("sin"); bcos = new button("cos"); btan = new button("tan"); bcls = new button("cls"); tf1 = new textfield("0"); tf1.seteditable(false); tf1.setcolumns(8); tf1.setbounds(100, 100, 250, 50); b0.setbounds(100, 130, 50, 50); b1.setbounds(150, 130, 50, 50); b2.setbounds(200, 130, 50, 50); b3.setbounds(250, 130, 50, 50); b4.setbounds(100, 180, 50, 50); b5.setbounds(150, 180, 50, 50); b6.setbounds(200, 180, 50, 50); b7.setbounds(250, 180, 50, 50); b8.setbounds(100, 230, 50, 50); b3.setbounds(150, 230, 50, 50); badd.setbounds(200, 230, 50, 50); bsub.setbounds(250, 230, 50, 50); bmul.setbounds(100, 280, 50, 50); bdiv.setbounds(150, 280, 50, 50); bequ.setbounds(200, 280, 50, 50); bsin.setbounds(300, 130, 50, 50); bcos.setbounds(300, 180, 50, 50); btan.setbounds(300, 230, 50, 50); bsqrt.setbounds(250, 280, 50, 50); bcls.setbounds(300, 280, 50, 50); add(b0); add(b1); add(b2); add(b3); add(b4); add(b5); add(b6); add(b7); add(b8); add(b9); add(badd); add(bsub); add(bmul); add(bdiv); add(bequ); add(bsin); add(bcos); add(btan); add(bsqrt); add(bcls); add(tf1); b0.addactionlistener(this); b1.addactionlistener(this); b2.addactionlistener(this); b3.addactionlistener(this); b4.addactionlistener(this); b5.addactionlistener(this); b6.addactionlistener(this); b7.addactionlistener(this); b8.addactionlistener(this); b9.addactionlistener(this); badd.addactionlistener(this); bsub.addactionlistener(this); bmul.addactionlistener(this); bdiv.addactionlistener(this); bequ.addactionlistener(this); bsqrt.addactionlistener(this); bsin.addactionlistener(this); bcos.addactionlistener(this); btan.addactionlistener(this); bcls.addactionlistener(this); } public void actionperformed(actionevent ae) { if(ae.getsource() == b0) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b0.getlabel()); else tf1.settext(tf1.gettext()+b0.getlabel()); temp = 0; } if(ae.getsource() == b1) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b1.getlabel()); else tf1.settext(tf1.gettext()+b1.getlabel()); temp = 0; } if(ae.getsource() == b2) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b2.getlabel()); else tf1.settext(tf1.gettext()+b2.getlabel()); temp = 0; } if(ae.getsource() == b3) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b3.getlabel()); else tf1.settext(tf1.gettext()+b3.getlabel()); temp = 0; } if(ae.getsource() == b4) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b4.getlabel()); else tf1.settext(tf1.gettext()+b4.getlabel()); temp = 0; } if(ae.getsource() == b5) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b5.getlabel()); else tf1.settext(tf1.gettext()+b5.getlabel()); temp = 0; } if(ae.getsource() == b6) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b6.getlabel()); else tf1.settext(tf1.gettext()+b6.getlabel()); temp = 0; } if(ae.getsource() == b7) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b7.getlabel()); else tf1.settext(tf1.gettext()+b7.getlabel()); temp = 0; } if(ae.getsource() == b8) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b8.getlabel()); else tf1.settext(tf1.gettext()+b8.getlabel()); temp = 0; } if(ae.getsource() == b9) { zero(); if(tf1.gettext().equals("0")) tf1.settext(b9.getlabel()); else tf1.settext(tf1.gettext()+b9.getlabel()); temp = 0; } if(ae.getsource() == bsqrt) { sqr = double.parsedouble(tf1.gettext()); tf1.settext(double.tostring(math.sqrt(sqr))); } if(ae.getsource() == bsin) { s = double.parsedouble(tf1.gettext()); tf1.settext(double.tostring(math.sin(s))); } if(ae.getsource() == bcos) { c = double.parsedouble(tf1.gettext()); tf1.settext(double.tostring(math.cos(c))); } if(ae.getsource() == btan) { ta = double.parsedouble(tf1.gettext()); tf1.settext(double.tostring(math.tan(ta))); } if(ae.getsource() == badd) { add together = integer.parseint(tf1.gettext()); tf1.settext(""); t = "+"; } if(ae.getsource() == bsub) { sub = integer.parseint(tf1.gettext()); tf1.settext(""); t = "-"; } if(ae.getsource() == bmul) { mul = integer.parseint(tf1.gettext()); tf1.settext(""); t = "*"; } if(ae.getsource() == bdiv) { div = integer.parseint(tf1.gettext()); tf1.settext(""); t = "/"; } if(ae.getsource() == bequ) { if(t == "+") { int add1 = integer.parseint(tf1.gettext()); int add2 = add together + add1; tf1.settext(string.valueof(add2)); } else if(t == "-") { int sub1 = integer.parseint(tf1.gettext()); int sub2 = sub - sub1; tf1.settext(string.valueof(sub2)); } else if(t == "*") { int mul1 = integer.parseint(tf1.gettext()); int mul2 = mul * mul1; tf1.settext(string.valueof(mul2)); } else if(t == "/") { int div1 = integer.parseint(tf1.gettext()); int div2 = div / div1; tf1.settext(string.valueof(div2)); } if(temp == 0) temp = 1; } if(ae.getsource() == bcls) tf1.settext("0"); if(ae.getsource() == bdec) { string s = tf1.gettext(); for(int = 0; < s.length(); i++) { if((s.charat(i)) == '.') break; else tf1.settext(tf1.gettext()+bdec.getlabel()); } } } void zero() { if(temp == 1) tf1.settext("0"); } }
look @ these lines:
badd = new button("+"); bsub = new button("-"); bsub = new button("*"); bdiv = new button("/");
you're assigning value bsub
twice, not assigning value bmul
, it's still null.
when dereference bmul
@ line 42:
bmul.setbounds(100, 280, 50, 50);
... that's throwing nullpointerexception
. suspect start of sec line above should be:
bmul = new button("*");
i strongly advise stick 1 statement per line. i'd advise break code smaller chunks - example, 1 method initialize digits, operators etc.
java applet
No comments:
Post a Comment