Results 1 to 12 of 12
  1. #1
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default ArrayIndexOutOfBoundsException - possible bug in JTextField?

    Hey all,

    I keep getting a rather weird glitch:
    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)
    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)
    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

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,181
    Rep Power
    20

    Default

    Are you using a non-standard font?

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by berkeleybross View Post
    Hey all,

    I keep getting a rather weird glitch:
    [code]
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
    Are you sure you don't have an array of some sort with less than ten elements and pass that array on to some built-in class method?

    kind regards,

    Jos

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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:
    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);
                }
            });
        }
    }
    Actually trying to reproduce the problem is surprisingly difficult, but accidentally doing it is stupidly easy.

    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 :)

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,181
    Rep Power
    20

    Default

    So you're adding text at the same time as drag/select that text?

    Might I suggest not doing that...;)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by berkeleybross View Post
    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
    I ran your code but I couldn't manage to make it crash ... the code is so simple and looks ok to me ...

    kind regards,

    Jos

  8. #8
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Jos -
    Actually trying to reproduce the problem is surprisingly difficult, but accidentally doing it is stupidly easy.
    :P

    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

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    So you're adding text at the same time as drag/select that text?

    Might I suggest not doing that...;)
    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

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by berkeleybross View Post
    Jos - :P

    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
    Sorry, I am not able to reproduce the said bug: I typed like a madman for a couple of minutes while trying to continuously select parts of the text. All went fine and my wife is frowning at me now ... ;-)

    kind regards,

    Jos

  12. #12
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Heh, oh well thanks for trying.

    I guess I'll just catch the exception and ignore it for now. If it causes signifcant problems I will come back and ask for more help :)

    Thanks for helping,
    berkeleybross

Similar Threads

  1. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 07:50 PM
  2. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 06:36 PM
  3. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  4. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 06:16 PM
  5. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •