swing - JFrame Error java.lang.nullpointerexception -
i have been trying create jframe
calculator getting error java.lang.nullpointer.exception
. says have problem in line say:
guicalc go = new guicalc();
here of code:
package home.personalprojects.jordan; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class guicalc extends jframe { private jbutton calculate; private jtextfield num1field, num2field; private jcombobox operationbox; private jlabel label1, label2, label3; private string[] operationposs = {"+", "-", "*", "/"}; string operation; int num1, num2, answer; public guicalc(){ super("calculator"); setlayout(new flowlayout()); operationbox = new jcombobox(operationposs); calculate = new jbutton("calculate"); calculate.settooltiptext("enter 2 numbers , select operation find answer"); label1 = new jlabel("number 1: "); num1field = new jtextfield("", 10); label2 = new jlabel("number 2: "); num2field = new jtextfield("", 10); operationbox.additemlistener( new itemlistener(){ public void itemstatechanged(itemevent event){ if(event.getstatechange() == itemevent.selected){ int temp; temp = operationbox.getselectedindex(); switch(temp){ case 0: answer = num1 + num2; case 1: answer = num1 - num2; case 2: answer = num1 * num2; case 3: answer = num1 / num2; } } } }); calculate.addactionlistener( new actionlistener(){ public void actionperformed(actionevent event){ joptionpane.showmessagedialog(null, string.format("the answer: " + answer, event.getactioncommand())); } } ); add(label1); add(num1field); add(label2); add(operationbox); add(label3); add(num2field); add(calculate); } public static void main(string[] args) { guicalc go = new guicalc(); go.setdefaultcloseoperation(jframe.exit_on_close); go.setsize(500,500); go.setvisible(true); } }
when run code get...
exception in thread "main" java.lang.nullpointerexception @ java.awt.container.addimpl(container.java:1086) @ java.awt.container.add(container.java:998) @ javax.swing.jframe.addimpl(jframe.java:562) @ java.awt.container.add(container.java:410) @ testcalc.testcalc.<init>(testcalc.java:79) @ testcalc.testcalc.main(testcalc.java:87)
which points
add(label3);
which suggest label3
has not being initialised.
Comments
Post a Comment