Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OverStack?

 
Berty Ahern
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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()
{
try
{
//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);
salesTab.setLayout(layout);
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");

salesTab.add(makeSaleBtn);
salesTab.add(exitBtn);
salesTab.add(nextBtn);
salesTab.add(previousBtn);
salesTab.add(clearBtn);
salesTab.add(cancelBtn);


JPanel browseTab = new JPanel();

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

JPanel editTab = new JPanel();


saveBtn = new JButton("Save");
editTab.add(saveBtn);
editTab.add(nextBtn);
editTab.add(previousBtn);
editTab.add(clearBtn);
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.
JFrame.setDefaultLookAndFeelDecorated(true);

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

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

//Display the window.
frame.pack();
frame.setVisible(true);
}

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() {
createAndShowGUI();

}
});
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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.
Thankx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic