Results 1 to 12 of 12
- 08-09-2010, 11:15 AM #1
ArrayIndexOutOfBoundsException - possible bug in JTextField?
Hey all,
I keep getting a rather weird glitch:
This occurs seemingly at random, when the caret is after the last character in a JTextField. (In this instance the JTextField had 10characters in it - so the array index out of bounds makes sense)Java Code:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10 at sun.font.FontDesignMetrics.charsWidth(FontDesignMetrics.java:492) at javax.swing.text.Utilities.getTabbedTextOffset(Utilities.java:381) at javax.swing.text.Utilities.getTabbedTextOffset(Utilities.java:302) at javax.swing.text.Utilities.getTabbedTextOffset(Utilities.java:286) at javax.swing.text.PlainView.viewToModel(PlainView.java:403) at javax.swing.text.FieldView.viewToModel(FieldView.java:263) at javax.swing.plaf.basic.BasicTextUI$RootView.viewToModel(BasicTextUI.java:1540) at javax.swing.plaf.basic.BasicTextUI.viewToModel(BasicTextUI.java:1089) at javax.swing.text.DefaultCaret.positionCaret(DefaultCaret.java:292) at javax.swing.text.DefaultCaret.adjustCaret(DefaultCaret.java:497) at javax.swing.text.DefaultCaret.adjustCaretAndFocus(DefaultCaret.java:485) at javax.swing.text.DefaultCaret.mousePressed(DefaultCaret.java:475) at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263) at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262) at java.awt.Component.processMouseEvent(Component.java:6260) at javax.swing.JComponent.processMouseEvent(JComponent.java:3255) at java.awt.Component.processEvent(Component.java:6028) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2475) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
None of the methods throwing an error in the stack trace above are mine, so i was wondering if anybody can confirm it is a bug in Java?
I realise that my first thought shouldnt be that its a bug with java and it is more likely a bug in my code, but I tested it on a blank form with just a JTextField in, and it still throws the error (About once in a hundred times).
I tried googling it, but for some reason ArrayIndexOutOfBounds is a fairly commonly asked question >.<
my java version is:
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)
- running on windows 7 32-bit
Thanks for your time,
berkeleybross
- 08-09-2010, 11:23 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Are you using a non-standard font?
- 08-09-2010, 11:37 AM #3
No, Im using the default font.
I've finally found a bug report here
The last post was in september last year, and it *says* the bug was fixed and will be released in a future SDK... but ive got a later version and it still occurs.
I dont want to just catch an arrayindexoutofbounds exception, incase i make a mistake later on and dont notice it...
berkeleybross
- 08-09-2010, 12:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2010, 12:24 PM #5
Hi Jos,
Fairly sure :S
Even if i did, wouldnt it show up somewhere in the stack trace?
this code throws the error as well:
Actually trying to reproduce the problem is surprisingly difficult, but accidentally doing it is stupidly easy.Java Code:public class Main { public static void main (String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame mainFrame = new JFrame ("demo"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.getContentPane().add(new JTextField ()); mainFrame.pack(); mainFrame.setVisible(true); } }); } }
As the bug i posted above says:
To reproduce:
-Run the program below
-start typing text in the text field
-while typing, keep drag-selecting text from the right to the left (selected text gets replaced by what you type)
-should get error
berkeleybross
ps woop 100th post here :)
- 08-09-2010, 12:28 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
So you're adding text at the same time as drag/select that text?
Might I suggest not doing that...;)
- 08-09-2010, 12:34 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2010, 12:34 PM #8
haha yeah its a bit of a weird description.
Its slightly more complicated then i've suggested, as the program is selecting the text.
Basically what im trying to make is a DatePicker. When the user presses a seperator key, such as "-" or "/" or tab, the program selects the next part of the date automatically, so when the user types it replaces the whole month instead of making the user delete the whole month.
It all works, but it throws the error when the user is typing the last part of the date.
berkeleybross
- 08-09-2010, 12:43 PM #9
Jos -
:PActually trying to reproduce the problem is surprisingly difficult, but accidentally doing it is stupidly easy.
It happens about once in a hundred attempts for me, but i assure you it does happen.
I just tried it now, to prove my sanity, and it took me about two minutes of constant typing whilst highlighted before it occured.
Maybe the fact I'm automatically highlighting the text means its slightly more likely to happen than otherwise, but in any case it shouldnt happen at all.
berkeleybross
- 08-09-2010, 12:45 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
It doesn't matter because processing keys and mouse movements and updating the text field all happens in one thread, the EDT; they're all handled one by one (very quickly, giving the impression that things happen at the same time). The code posted doesn't use other threads so all is sound.
kind regards,
Jos
- 08-09-2010, 12:56 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2010, 01:07 PM #12
Similar Threads
-
ArrayIndexOutOfBoundsException
By er1c550n20 in forum New To JavaReplies: 2Last Post: 04-07-2010, 06:50 PM -
ArrayIndexOutOfBoundsException
By flaskvacuum in forum New To JavaReplies: 6Last Post: 07-14-2009, 05:36 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
ArrayIndexOutofBoundsException help
By filly444 in forum New To JavaReplies: 9Last Post: 09-03-2008, 05:16 PM -
ArrayIndexOutOfBoundsException
By daredavil82 in forum New To JavaReplies: 2Last Post: 12-14-2007, 09:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks