tags:

views:

714

answers:

8

I'm currently working on a Java project and was having a problem with a stack overflow error. What happens is first the program reads in a file of about 1,500,000 words and adds it to an array. It then reads in a small file of about 600 words and adds it to an array. It checks how many words in the 600 word file occur in the other file. Each word in the big file is associated with a number. So when it finds a word in the big file it takes a copy of the word and its associated integer and adds it to an array. My problem is that I am getting a stack overflow error:

"AWT-EventQueue-0" java.lang.StackOverflowError

The thing is that when the small file is about 200 words the program runs fine. The last line the program has to execute is:

result.setPage("file:file for gui NEW.html");

(where result is an JEditorPane)

For some reason I get a stackoverflow error when the small file is 600 words but runs ok when it is 200 words. It runs the last line and produces this file but doesn't print it to the editor pane as that is when the exception kicks in.

Can anyone help to tell me why this may happen and how I could go about fixing it? Thanks.


The error in the console in full is:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.SunToolkit.getSystemEventQueueImplPP(Unknown Source)
    at sun.awt.SunToolkit.getSystemEventQueueImpl(Unknown Source)
    at java.awt.Toolkit.getEventQueue(Unknown Source)
    at java.awt.EventQueue.isDispatchThread(Unknown Source)
    at javax.swing.SwingUtilities.isEventDispatchThread(Unknown Source)
    at javax.swing.JComponent.revalidate(Unknown Source)
    at javax.swing.plaf.basic.
             BasicTextUI$RootView.preferenceChanged(Unknown Source)
    at javax.swing.text.View.preferenceChanged(Unknown Source)
    at javax.swing.text.BoxView.preferenceChanged(Unknown Source)
    at javax.swing.text.View.preferenceChanged(Unknown Source)
    at javax.swing.text.BoxView.preferenceChanged(Unknown Source)
    at javax.swing.text.View.preferenceChanged(Unknown Source)
    at javax.swing.text.BoxView.preferenceChanged(Unknown Source)
    (... repeating forever ...)

EDIT: So basically it seems that two controls in the GUI keep invoking each other's preferenceChanged() method.


The Gui seems like the most likly cause because when I run the program without the gui and print the contents of the file to the console instead it works fine. I've no idea what is actually causing the problem. I'm not using preference changed routine. Only thigs like setSize(), setVisible() etc. would that cause it?

+5  A: 

Do you have a recursive function somewhere? This is usually the source of stack overflows.

Additional info here.

Brian Rasmussen
A: 

You should check for recursion - both direct recursion (when a function calls itsself) and indirect recursion (when A calls B and B calls A again).

The easiest way to do this is to attach a debugger and look at the call stack at the moment you've got stack overflow.

sharptooth
A: 

Assuming you can attach a debugger, on Eclipse (sorry, I don't know Netbeans/Idea) you can then add an exception breakpoint for StackOverflowError. In the breakpoint view's toolbar, there is a button which has a blue J with an exclamation mark - "J!" - click this, then enter StackOverflowError and choose to suspend on uncaught exceptions only.

Perhaps this will provide some more context as to what is going wrong.

Rich
+1  A: 

Look at the stacktrace of the StackOverflowError. It will almost certainly show you one or two methods repeatedly calling itself/each other. Rewrite those methods so that this does not happen (or does not happen that often).

Michael Borgwardt
how do you check what the stacktrace is?
The Special One
The stacktrace is all the lines under the exception or error itself on the console - it tells you exactly where the problem occurred and is absolutely crucial for any debugging in Java
Michael Borgwardt
The stacktrace is printed in the question. In the end it starts to repeat the last 2.
The Special One
A: 

If it is a recursion issue then you can try to change your algorithm with a new one that uses an explicit stack instead of an implicit stack like BFS or DFS

idursun
+3  A: 

The stack overflow is in a GUI object's preferenceChanged() routine. The culprit is something your GUI is doing, your array usage is just the trigger.

Are you subclassing any GUI objects, especially one with your own preferenceChanged() function anywhere? Best guess is that it has something to do with displaying a scrollbar, since the exception depends on how many objects you're adding to this array. Check the docs very carefully for any GUI objects or events that you are working with.

HUAGHAGUAH
Hey. This seems like the most likly cause because when I run the program without the gui and print the contents of the file to the console instead it works fine. I've no idea what is actually causing the problem. I'm not using preference changed routine. Only thigs like setSize(), setVisible() etc.
The Special One
Time to debug your GUI then. Are you subclassing anything and/or reacting to GUI events?
HUAGHAGUAH
My gui is running a class (that works with a big file without the gui) and then it runs the line: result.setPage("file:file for gui NEW.html");If i put any print statements after it it prints them and then throws the exception.
The Special One
The stack trace lists the event queue, not main. That means a GUI event (setPage?) is being processed asynchronously when the overflow happens.Maybe your HTML is malformed. Identify exactly what the difference is between crashing and not, by removing large portions of your program if necessary.
HUAGHAGUAH
The difference is if I pass in a small file (say 300 words) it runs and displays fine in the editor pane ut when I pass a big file (say 800 words) it then throws the stackoverflow error.
The Special One
Also I don't know if it's the scrollbar but the only lines related to that are: JScrollPane scroll = new JScrollPane(result); scroll.setSize(500, 500); container.add(scroll); (where result is the editorPane and is 500,500), and container is the JFrame.
The Special One
I can't teach you how to debug.The difference between 300 and 800 (or 200 and 600) words isn't very exact. Remove your code and just display fixed amounts of text, or remove GUI features (like scrolling or sizes). Try to find the smallest difference between something that works and what doesn't.
HUAGHAGUAH
+4  A: 

I think you have ran into a variation of this issue:

http://www.jdocs.com/harmony/5.M5/javax/swing/text/BoxView.html#M-layout(int,int)

protected void layout ( int width, int height )

This method may cause stack overflow if upon each layout try a child changes its preferences, i.e. preferenceChanged is called.

I'm not sure how you managed do achieve this but try to not to change prefences of childs on layouts. I'm sure that someone with more experience with swing than me could provide a more valuable answer.

DrJokepu
Would you be able to expand on this please. It seems like a possible soloution to my problem.
The Special One
The Special One: I'm sorry, it's been a while since I've used Swing and I don't want write things that are not true. You should ask someone with more Swing experience. I'll edit your question a little bit, hopefully this will attract the attentoion of people with more Swing knowledge.
DrJokepu
+1  A: 

I would check the file that's being output since the recursion seems to be happening in the JEditorPane rather than in your code - does this file show up in a normal web browser okay? Keep in mind the the JEditorPane is not the most advanced component in the world and may choke if you're trying to show something complex.

I've also experienced layout errors in JEditor Pane which occur seemingly at random and I've never found the bottom of, although this is usually when I'm editing the contents of the pane rather than just showing a web page.

If you're changing the size of the window whilst you're loading the file it may cause the kinds of errors that you're describing - I would try and just load the file into the editor pane without altering it.

Brother Logic