Here is my code(for an applet):
class Calculator extends JApplet{
JTextField numberbox; //all neccessary components
JLabel answerbox;
JButton add;
JButton subtract;
JButton multiply;
JButton divide;
JButton enter;
double anumber, bnumber; //for calculation
int function = 1;
String answer;
public void init(){ //startup the applet
try{
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
guiInit();
}
});
}
catch(Exception e){
System.out.println("Can't create: "+e);
}
}
public void guiInit(){
setLayout(null); //absolute positioning
numberbox = new JTextField(""); //setting text, size and position of all components
numberbox.setBounds(5,5,300,200);
answerbox = new JLabel("");
answerbox.setBounds(700, 200, 50,20);
add = new JButton("+");
add.setBounds(5, 210, 50, 20);
subtract = new JButton("-");
subtract.setBounds(60, 210, 50, 20);
multiply = new JButton("x");
multiply.setBounds(115, 210, 50, 20);
divide = new JButton("/");
divide.setBounds(170, 210, 50, 20);
enter = new JButton("Enter");
enter.setBounds(225, 210, 50, 20);
add.addActionListener(new ActionListener(){//action listeners for each button
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){//if nothing is in the text box, use the last answer instead
anumber = Double.parseDouble(answer);
}
else{
anumber = Double.parseDouble(numberbox.getText());//read the text box
}
numberbox.setText("");//clear the box for the next value
function = 1;//set the operator to be used
}
});
subtract.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){
anumber = Double.parseDouble(answer);
}
else{
anumber = Double.parseDouble(numberbox.getText());
}
numberbox.setText("");
function = 2;
}
});
multiply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){
anumber = Double.parseDouble(answer);
}
else{
anumber = Double.parseDouble(numberbox.getText());
}
numberbox.setText("");
function = 3;
}
});
divide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent le){
if(numberbox.getText() == ""){
anumber = Double.parseDouble(answer);
}
else{
anumber = Double.parseDouble(numberbox.getText());
}
numberbox.setText("");
function = 4;
}
});
enter.addActionListener(new ActionListener(){//if the enter button is pressed
public void actionPerformed(ActionEvent le){
bnumber = Double.parseDouble(numberbox.getText());//set the second value
numberbox.setText("");//clear the box for next question
if(function == 1){//checking to see what operator should be used
answer = Double.toString(anumber+bnumber);
answerbox.setText(answer);//displaying the answer
}
else if(function ==2){//other operators
answer = Double.toString(anumber-bnumber);
answerbox.setText(answer);
}
else if(function == 3){
answer = Double.toString(anumber*bnumber);
answerbox.setText(answer);
}
else if(function == 4){
answer = Double.toString(anumber/bnumber);
answerbox.setText(answer);
}
else{
answerbox.setText("Error");
}
}
});
getContentPane().add(numberbox);//adding the components to the pane
getContentPane().add(answerbox);
getContentPane().add(add);
getContentPane().add(subtract);
getContentPane().add(multiply);
getContentPane().add(divide);
getContentPane().add(enter);
}
}
sorry for the long code post but I really have no idea what is causing this. I have used applets before but never gotten this exception. I really dont understand any of the research I did on this topic either.
from what I understand, it had something to do with the exception, and therefore I would assume the guiInit() method.
I really dont understand reflection or what in my code uses it
thanks for any possible help, I am obviously new to java.