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?
Do you have a recursive function somewhere? This is usually the source of stack overflows.
Additional info here.
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.
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.
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).
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.
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.
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
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.
Related
I have a method leftup which has an onClick listener from a button. The purpose of method leftup is to do the following:
Get an array from resources xml
(Which contains names of my other activities)
Turn array into an array list
Remove a string from that array
(The current class name)
Convert the list back into an array
Get a random value from that array
Execute method goToActivity with parameter of that random value
I have spent hours on this task so far, because the logic of the code seems fine. The problem is, when I run the code, I get the infamous error:
Unfortunately, "App_Name" has stopped
Here is my code for method onClick and goToActivity:
Here is my array in the resources:
By the way, string activity state is the name of the activity :
I am sure the exception is in the method leftup, as none of the Toasts get executed, and the error happens immediately as I click on the button.
Thank you so much for all your help, I really appreciate it.
A fellow programmer,
{Rich}
Stack trace:
https://gist.github.com/anonymous/dcd63f51289aaf1a1933
Here is the stack trace because I couldn't format it, this is the git hub page.
I figured it out! After three long hours of tweaking the code, I finally got it! The arrayList had to be a linkedList. Thats it! Because, arrayList's aren't as flexible, and so one cannot remove or add items to them. I hope this answer helps, and saves you three long hours!
java.lang.UnsupportedOperationException
java.lang.reflect.InvocationTargetException
are the exceptions that happen on this issue. Just make sure to use a more flexible container. :)
{Rich}
I'm developing a plug-in for the Eclipse platform. This plug-in will be used to give information about the line of Java source code currently being debugged.
When debugging a Java program, as you hit a breakpoint, Eclipse switches to the standard Debug perspective. Inside this perspective, apart from the standard Console output, the stack trace and various other views, you can see source code of the Java program currently being debugged. Inside this 'source code view', you can see a highlighted line, which is the line of code currently being debugged/evaluated. This highlighted line of code is what I want to access.
Assuming I know when the debugger is running (I assess that through a DebugBreakpointListener class that implements IJavaBreakpointListener), I need to 'ask questions' to the debugger. What, I imagine, I will need, is to somehow ask the debugger directly either for the line of code it is currently highlighting/debugging/evaluating or for the line number of the said line of code.
I'm making a static access to the JDIDebugModel to add the Java Breakpoint Listener:
JDIDebugModel.addJavaBreakpointListener(new DebugBreakpointListener);
I thought I could access the debugger with static references to JDIDebugPlugin but I've yet to find what I'm looking for.
At Part 3 of this research paper, the authors suggested that:
The Eclipse Java debugger is built upon the API of Java Debug Interface (JDI), which is part of the Java Development Toolkit. This API enables adding requests to monitor JVM events such as BreakpointEvent. When an event occurs, the debugger gets a notification and the thread in which this event took place can be obtained. For each frame in the stack trace of this thread the following information can be obtained:
• The source Java file in which the execution at this frame has taken place (or null if the source is not available).
• The method and line number (if available).
• The this object or null if the method is static.
The Eclipse debugger uses this information when a breakpoint is hit. It shows the stack trace for the suspended thread in the ”Debug” view. For the selected frame in this trace, Eclipse highlights the corresponding line number in its source file, and displays the this variable in the ”Variables” view.
This bulletpoint-listed things are exactly what I'm looking for.
Unfortunately, I can't find detailed documentation on how to 'plug in' to the debugger.
If someone can give me information, point me to information or a sample code, or maybe provide me with contact information of someone from the Eclipse JDI project, it would be immensely appreciated.
Thanks in advance.
------Update & Answer:------
With the help of greg-449's answer, I did exactly what I wanted to do. Here's what I did:
The aformentioned breakpoint listener I wrote implements the interface method breakpointHit, which is as follows:
#Override
public int breakpointHit(IJavaThread thread, IJavaBreakpoint breakpoint) {
System.out.println("Just hit a breakpoint!");
// Save pointers to the thread & breakpoint for future use.
return 0;
}
With the pointers to the thread and breakpoint objects saved in one of my objects, I could query them to get up-to-date information on the state of the frame stack, the thread and about the particular breakpoint that I've hit. I can get the namea dn path of the class the debugger is currently debugging by calling:
IStackFrame topStackFrame = thread.getTopStackFrame();
int debuggedLineNumber = topStackFrame.getLineNumber();
String debuggedClassPath = topStackFrame.getLaunch().getSourceLocator().getSourceElement(thread.getTopStackFrame()).toString();
This was exactly what I was looking for. I imagine I will need to read the source code files manually, run them through a tokenizer by having the 'newline' character as a delimiter and get the corresponding token to read that specific line.
There is a huge amount of information available in the IJavaThread and IJavaBreakpoint arguments passed to the breakpointHit method of the IJavaBreakpointListener which should contain this information.
I think for breakpoints which have a line number (not all do) the IJavaBreakpoint argument also implements ILineBreakpoint containing the line information.
I'm trying to remove a set of child elements from a parent element using VTD-XML.
Unfortunately after removing an element, it leaves behind the new line that the removed element previously occupied. This behaviour is also observed by a reader of an article on VTD-XML by the VTD-XML author here. I'm trying to work out how to remove this new line.
I managed to achieve a modicum of success by manipulating the length value stored in the underlying 64-bit VTD token to cover the new line character (additional 2 bytes). Code snippet is as follows:
// XMLModifier modifier
modifier.remove(vn.getElementFragment()+0x200000000L);
I've tested that this works well on the old_cd.xml provided in ex_16 of the VTD-XML Examples.
However when I try this same approach on my working file, a ModifyException error is thrown when I attempt to call modifier.output(), specifically it is thrown by modifier.check2().
Questions
1. Why would the above approach cause check2() to fail? I don't think I'm overflowing the bits on the VTD token, file is < 2MB. See Update.
2. Is there a better approach to remove the remaining new line?
I'm still fairly new to VTD-XML so I would greatly appreciate any advice and insight and learn from more experienced users.
Thanks for your help.
Update
Wow, in the process of writing this question I realise that I forgot to consider the different character encodings and updating the adjusting long value to 1 byte fixed the check2() problem! (another reason to take the time to pause and rethink/write out the problem).
I'd still like to know from more experienced users if there are better approaches to this.
To answer your question, I think this needs to be done at the API level and it needs to take care a few extra details, like the options to remove all surrounding white spaces or none of the white spaces. It needs to be done in the next release...
I am using a Java SWT StyledText control to display some text on Windows. However, the performance sucks because the text I am displaying has lots of commas. Here is an example of a line of text I am trying to display:
1,2,3,4,5,1,
The Java SWT code calls into the standard Windows Uniscribe ScriptItemize function to split this text into runs and gets back 12 runs! Each character is its own run! This kills the performance of the SWT drawing code to have to draw the characters one by one.
I have raised Eclipse bug 352927 to cover this issue. Does anyone have experience with Uniscribe who could comment on how to avoid these one character runs?
The settings for the ScriptItemize method come from a SCRIPT_CONTROL struct. However, the
defn of this struct used by SWT is missing the fMergeNeutralItems field. This
sounds promising to me but I haven't managed to find anything clear online
about whether this field would merge together the returned runs for my comma
heavy text.
Setting fMergeNeutralItems in the SCRIPT_CONTROL struct fixes this issue. See the SWT bugzilla bug for more details.
This question already has answers here:
Does Eclipse have line-wrap
(12 answers)
Closed 7 years ago.
I already have word wrap for Eclipse PHP (How can I get word wrap to work in Eclipse PDT for PHP files?). What about Eclipse Java? Is there a similar tool to wrap Java code lines?
Ctrl+Shift+F will format a file in Eclipse, breaking long lines into multiple lines and nicely word-wrapping comments. You can also highlight just a section of text and format that.
I realize this is not an automatic soft/hard word wrap, but I don't think the question was asking for anything fancy.
Just like to point out that word wrapping is a terrible idea in code. I suppose everyone has their own style, but typically you should refactor or rethink blocks of code that take much more than a single average horizontal screen. (about 1280 pixels)
Of course there are always exceptions... like verbose exceptions for example, but these are not critical for code understanding.
Scanning a line that goes beyond a single horizontal screen, or having to read a thought that stacks on multiple lines will make your code much more difficult to figure out.
Additionally, line counts will be weird, as word wrapping will make a single line seem like it takes up 2, 3, or shudder the thought... 4 lines of code.
As far as comments go, line returns should not be much of a problem for anyone.
Also keep in mind, word wrapping code is not a common practice, and you are essentially saying that everyone reading your code should turn word wrapping on, which is, I'm sorry to say, an unnecessarily painful thing to make others working with your code do....
If nesting is causing you the need for word wrap... well this is also a very bad code smell.
That's my two-cents.
This is for anyone who is having the following problem with the "Ahtik" word-wrap plugin in Texlipse: the plugin installs fine, but selecting the "word-wrap" option using the context menu (or pressing ctrl+alt+w) causes the text to wrap momentarily, and then just flick back to being unwrapped (this probably includes anyone using Windows 7).
Here is a fix...straight from the developer himself! I emailed Ahti Kitsik in a last-ditch attempt to find a way round, and was amazed when he actually found a solution, and sent it to me the next day. He suggested that in return I could just make this information widely available.
So, the way to fix it as follows:
Look up the texlipse plugin location. This will probably be in the Eclipse folder, and the address will probably end: ...\eclipse\plugins\net.sourceforge.texlipse_1.5.0.
Backup the plugin.xml file there just in case, and then edit the original by deleting the following chunks of code:
-Firstly, the "action" (starting at line 843):
action
class="net.sourceforge.texlipse.actions.TexWordWrapAction"
definitionId="net.sourceforge.texlipse.commands.texWordWrap"
icon="icons/wrap.gif"
id="net.sourceforge.texlipse.actions.texWordWrap"
label="W&rap text"
menubarPath="net.sourceforge.texlipse.menus.latex/latexGroup"
style="toggle"
toolbarPath="latexGroup"
tooltip="Use word wrap"
-Secondly, the "command" (starting at line 972):
command
categoryId="net.sourceforge.texlipse.latexEditingCategory"
name="Wrap text"
id="net.sourceforge.texlipse.commands.texWordWrap"
(When you have done this, the file should be 1257 lines long, instead of 1271).
Finally, Ahti says: "Start eclipse with -clean command line argument to force reloading of plugin.xml." (Personally I had no idea how to do this, so I just removed and reinstalled his plugin, then exited Eclipse, and edited the code before I restarted it).
The function should work fine now. Hope this helps someone. And thanks again Ahti!
In the meantime, there has been an fork to Ahtik's word wrap plugin which, I must say, works very nice and fixes jumbled line numbering bug: Eclipse Word-Wrap Plug-In
Open Eclipse -> Help -> Install New Software -> Click "Add.." button and copy this line "http://dev.cdhq.de/eclipse/updatesite/luna/" into field Name and Location -> Click OK
Check -> Line Number Ruler and Word Wrap Feature -> Next -> Next -> Finish -> Restard Eclipse!