LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2004, 02:44 AM   #1
nro
Member
 
Registered: Oct 2003
Distribution: Mandrake 9
Posts: 46

Rep: Reputation: 15
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
 
Old 09-04-2004, 03:47 AM   #2
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
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

Last edited by bruce ford; 09-04-2004 at 03:49 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting Java from shell script (Exception in thread "main") rolf_mueller Linux - Software 5 10-30-2004 02:11 AM
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window serve paragvd Programming 4 10-05-2004 03:35 AM
Exception in thread "main" java.lang.NoClassDefFoundError: Lobais Linux - Software 19 06-23-2004 08:42 AM
Exception in thread "main" java.lang.NoClassDefFoundError: melinda_sayang Programming 2 04-27-2004 11:49 AM
I cannot use "java chat". Browser says plugin required "x-java-vm". jdruin Linux - Software 4 04-18-2004 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration