1

I'm new into Java... and I've got this idea of building a scientific calculator and after I implemented some of the actionListeners I've got the following error.

Exception in thread "main" java.lang.StackOverflowError at java.awt.Insets.(Insets.java:103) at sun.awt.windows.WToolkit.getScreenInsets(Native Method) at sun.awt.windows.WToolkit.getScreenInsets(WToolkit.java:567) at java.awt.Window.init(Window.java:498) at java.awt.Window.(Window.java:536) at java.awt.Frame.(Frame.java:420) at javax.swing.JFrame.(JFrame.java:224) at GUI.(GUI.java:29) at actionListeners.(actionListeners.java:9) at GUI.(GUI.java:9) at actionListeners.(actionListeners.java:9) at GUI.(GUI.java:9) at actionListeners.(actionListeners.java:9) at GUI.(GUI.java:9) at actionListeners.(actionListeners.java:9) at GUI.(GUI.java:9) at actionListeners.(actionListeners.java:9)

and this error keeps showing alot . What could be the problem ? 

Here is the code

public GUI() {
    super("Calculator");
    setLayout(new GridLayout(5,2));

    result = new JTextArea();
    result.setEditable(false);
    add(result);
    divide.addActionListener(actionListeners);
    add(divide);
    multiply.addActionListener(actionListeners);
    add(multiply);
    substract.addActionListener(actionListeners);
    add(substract);
    sum.addActionListener(actionListeners);
    add(sum);
    for(int i=0;i<=numberButtons.length-1;i++)
 {
     numberButtons[i]= new JButton(Integer.toString(i));}

    add(numberButtons[7]);
    add(numberButtons[8]);
    add(numberButtons[9]);
    add(C);
    add(CE);
    add(numberButtons[4]);
    add(numberButtons[5]);
    add(numberButtons[6]);
    add(sqrt);
    add(cubic);
    add(numberButtons[1]);
    add(numberButtons[2]);
    add(numberButtons[3]);
    add(percentage);
    add(divideByOne);
    add(numberButtons[0]);
    add(point);
    add(square);
    add(OK);

}
public void setTextResult(String a) {
    result.setText(a);
  }
}


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.naming.spi.DirStateFactory.Result;

public class actionListeners implements ActionListener {

Functions fn = new Functions();
GUI go = new GUI();


public void actionPerformed(ActionEvent e) {
    String x = e.getActionCommand();
    switch (x) {
    case "/":
        fn.divide(fn.getA(), fn.getB());
        go.setTextResult(Double.toString(fn.getResult()));
        break;
    case "*":
        fn.multiply(fn.getA(), fn.getB());
        break;
    case "-":
        fn.substract(fn.getA(), fn.getB());
        break;
    case "+":
        fn.sum(fn.getA(), fn.getB());
        break;
    case "+-":
        fn.divide(fn.getA(), fn.getB());
        break;
    case "C":
        go.setTextResult(" ");
        break;
    case "CE":
        go.setTextResult(" ");
        fn.setResult(0);
        break;
    case "sqrt":
        fn.sqrt(fn.getA());
        break;
    case "OK":
        go.setTextResult(Double.toString(fn.getResult()));
        break;
    case "SQRT":
        break;
    case "%":
        break;
    case "x^2":
        break;
    case "x^3":
        break;
    case "1/x":
        break;
    default:
        go.setTextResult("Eroare");
        break;
    }
}
}
Dan
  • 2,701
  • 1
  • 29
  • 34
CiucaS
  • 2,010
  • 5
  • 36
  • 63
  • can you be more specific about where exactly the error accour? – Kick Buttowski Dec 09 '13 at 18:29
  • when I try to run the code, I've posted the log from Console. PS: forgot to add the Functions class, but I don't think that could be the problem. – CiucaS Dec 09 '13 at 18:35
  • StackOverflow usually means that you have a recursive function call that does not stop. Apparently there is one at rule 9... – Haneev Dec 09 '13 at 18:35
  • @ Haneev... yes I've read that it's related to some kind of a recursive function, but I can't see the problem in my case. – CiucaS Dec 09 '13 at 18:37
  • possible duplicate of [What is a stack overflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error) –  Mar 27 '14 at 05:47

2 Answers2

1

You havent presented your complete code but you appear to have a cyclic dependency between the GUI and actionListeners classes. Each one requires the other to be instantiated resulting in the stacktrace as shown. Just create the required instance of actionListeners in GUI but not the other way around.

Suggestion:

  • Consider using an Action for shared functionality between buttons
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I understand, the rest of the code is a bunch of functions for the math operations, I don't think the problem is there. Anyway I understand what you said, but then how I get my answer writen in the JTextArea? I need an object of GUI into the actionListeners for that. – CiucaS Dec 09 '13 at 18:42
  • I have no idea how to do that. – CiucaS Dec 09 '13 at 18:55
  • Read [Passing Information to a Method or a Constructor](http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – Reimeus Dec 09 '13 at 20:02
0

first of all what is your java version you are using strings in switch statement.It is implemented in java 7 and in your code addActionListener(actionListeners) you have to pass the object of ActionListener object.check those two..

prasad
  • 1,277
  • 2
  • 16
  • 39