Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • fred rosenberger


Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, My program has an overstack error. I know that this happens when something keeps on happening but I don't know where???
Also could you please suggest how I would Read the info in my file into an array?
here is the code:

public class TabDemo extends JFrame
private Container c;
private GridLayout layout;
final static String SALESPANEL = "New Sale";
final static String BROWSEPANEL = "Browse Books";
final static String EDITPANEL = "Edit Book Information";
final static String STATSPANEL = "Order Details";

private JButton makeSaleBtn,saveBtn,exitBtn,nextBtn,previousBtn,clearBtn,cancelBtn;
FileOutputStream outputBook; //Stream to create file

ObjectOutputStream objSaveBook; //Stream to save an object
TabDemo thisApp = new TabDemo();

public void openStream()
//Create file and object output streams
outputBook = new FileOutputStream("Books.txt");
objSaveBook = new ObjectOutputStream(outputBook);
catch(Exception error)
System.err.println("Error opening file");

public void addComponentToPane(Container pane) {
JTabbedPane tabbedPane = new JTabbedPane();

//Create the "cards".
JPanel salesTab = new JPanel() {

public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += 200;
size.height += 200;
return size;

c = getContentPane();
layout = new GridLayout(2,3);
makeSaleBtn = new JButton("Make Sale");

exitBtn = new JButton("Exit");
nextBtn = new JButton("Next");
previousBtn = new JButton("Previous");
clearBtn = new JButton("Clear");
cancelBtn = new JButton("Cancel");


JPanel browseTab = new JPanel();

browseTab.add(new JTextField("TextField", 20));

JPanel editTab = new JPanel();

saveBtn = new JButton("Save");
JPanel statsTab = new JPanel();

statsTab.add(new JButton("Button 12"));
statsTab.add(new JButton("Button 23"));
statsTab.add(new JButton("Button 34"));

tabbedPane.addTab(SALESPANEL, salesTab);
tabbedPane.addTab(BROWSEPANEL, browseTab);
tabbedPane.addTab(EDITPANEL, editTab);
tabbedPane.addTab(STATSPANEL, statsTab);

pane.add(tabbedPane, BorderLayout.CENTER);

* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
private static void createAndShowGUI() {
//Make sure we have nice window decorations.

//Create and set up the window.
JFrame frame = new JFrame("TabDemo");

//Create and set up the content pane.
TabDemo demo = new TabDemo();

//Display the window.

public static void main(String[] args) {
TabDemo thisApp = new TabDemo();
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

author and iconoclast
Posts: 24204
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Celebrity names are not OK. You can change your display name here. Thanks!

Now, your problem is that TabDemo has a member variable "thisApp" of type TabDemo, and it's initialized with "new TabDemo()". That means that every time you create a TabDemo, while it's being initialized, another TabDemo will be created. Of course, while that one is being initialized, another one will be created -- and so on, until you get a stack overflow.

That's OK, though, since this member is never used for anything. Instead, your main() routine has its own local variable, also named thisApp(), and that's the one that's actually used.

There are a number of other problems with this code; in particular, createAndShowGUI() creates yet another instance of TabDemo, rather than using the one created in main(). But why not try to work on this yourself for a while, and then come back if you get stuck again.
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a member variable defined called thisApp in your TabDemo class...

So, when you instantiate a TabDemo object, it instantiates a TabDemo object to set its thisApp property, which, in turn instantiates a TabDemo object, which instantiates a TabDemo object! Oh no, I've gone cross-eyed!
Berty Ahern
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey that did the job, no more Overstacking. I will start on fixing the createAndShowGUI() problem now. I will prob have some more questions on that later on.
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic