LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java error "Exception in thread "main" java.lang.StackOverflowError" (https://www.linuxquestions.org/questions/programming-9/java-error-exception-in-thread-main-java-lang-stackoverflowerror-226454/)

nro 09-04-2004 02:44 AM

Java error "Exception in thread "main" java.lang.StackOverflowError"
 
I have just created this simple program to progressively get closer to the value of pi and display it. I can't figure out a solution for my problem. Here is my code(sorry for length):
Code:

package PiCounter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class PiGUI extends JFrame {
   
    JTextField piDisplay  = new JTextField();
    JTextField calcDisplay = new JTextField();
    JLabel calcLabel      = new JLabel("# of calculations");
    JLabel piLabel        = new JLabel("Value of PI");
   
    Calculator calc = new Calculator(this);
   
    PiGUI() {
        // Frame listener - closeing window
        addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});
       
        // Configure GUI layout
        Container inter = getContentPane();
        inter.setLayout ( null );
       
        // Set the box not editable
        piDisplay.setEditable(false);
        calcDisplay.setEditable(false);
       
        // Set the box background
        piDisplay.setBackground(Color.black);
        calcDisplay.setBackground(Color.black);
       
        // Set the box forground
        piDisplay.setBackground(Color.black);
        calcDisplay.setForeground(Color.red);
       
        // Add components to GUI
        inter.add(piDisplay);
        inter.add(calcDisplay);
        inter.add(piLabel);
        inter.add(calcLabel);
       
        piLabel.setBounds(5,5,150,20);
        piDisplay.setBounds(5,30,150,40);
        calcLabel.setBounds(5,75,150,20);
        calcDisplay.setBounds(5,100,150,40);
       
        setSize(170,170); // Size of window
        setTitle("PI Calculator"); // Title of window
        setVisible(true); // Set visible
        setResizable(false); // Disallow resize
    }
}

class Calculator {
    PiGUI actionGUI;
   
    int calcCounter  = 0;
    double denomCount = 3;
    double pi        = 4;
   
    public Calculator(PiGUI tempGUI) {
        actionGUI = tempGUI;
        calcSub();
    }
   
    public void calcSub() {
        pi -= (4/denomCount);
        denomCount += 2;
        calcCounter++;
        calcAdd();
    }
   
    public void calcAdd() {
        pi += (4/denomCount);
        denomCount += 2;
        calcCounter++;
        if((calcCounter % 100000) == 0) {
            actionGUI.piDisplay.setText(String.valueOf(pi));
            actionGUI.calcDisplay.setText(String.valueOf(calcCounter));
        }
        calcSub();
    }
}

public class PiCounter {
   
    public static void main(String[] args) {
        PiGUI piGUI = new PiGUI();
    }
   
}

And the error I am recieving is:
Exception in thread "main" java.lang.StackOverflowError

The display window isn't even popping up. This is most likely a simple problem that I am overlooking. That where some of you might be able to help me :-).

Thanks,
Mike

bruce ford 09-04-2004 03:47 AM

Hi,
Regardless of the feasibility and correctness of this neverending Pi calculation your stack isn't large enough to store all of your call frames of calcAdd() and calcSub(). Do your calculation iterative instead of recursive, which is easy in your case since you have no recursion anchor in your program - strange, but ok. So remove the calcAdd() and calcSub() calls from calcSub() and calcAdd()
and add a function like this to the class Calculator:
Code:

calculate() {
  while( true ) {
    calcSub();
    calcAdd();
    if((calcCounter % 100000) == 0) {
            actionGUI.piDisplay.setText(String.valueOf(pi));
            actionGUI.calcDisplay.setText(String.valueOf(calcCounter));
    }
  }
}

call this function then instead of calcSub().

BTW - it is bad habit to do all calculations in the constructor, because then things like this tend to occur :

Quote:

The display window isn't even popping up.
so long...
bruce


All times are GMT -5. The time now is 09:33 PM.