Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2004

Tip: Looking for answers? Try searching our database.

java.lang.IllegalArgumentException: Invalid location

Thread view: 
Carsten Sch?fer - 23 Sep 2004 16:22 GMT
I sometimes see the following exception:
java.lang.IllegalArgumentException: Invalid location
at javax.swing.text.JTextComponent.getSelectedText(Unknown Source)
at
javax.swing.text.JTextComponent$AccessibleJTextComponent.getSelectedText(Unknown
Source)
at
javax.swing.text.JTextComponent$AccessibleJTextComponent.caretUpdate(Unknown
Source)
at javax.swing.text.JTextComponent.fireCaretUpdate(Unknown Source)
at javax.swing.text.JTextComponent$MutableCaretEvent.fire(Unknown Source)
at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(Unknown
Source)
at javax.swing.text.DefaultCaret.fireStateChanged(Unknown Source)
at javax.swing.text.DefaultCaret.changeCaretPosition(Unknown Source)
at javax.swing.text.DefaultCaret.handleMoveDot(Unknown Source)
at javax.swing.text.DefaultCaret.moveDot(Unknown Source)
at javax.swing.text.DefaultCaret$UpdateHandler.insertUpdate(Unknown Source)
at javax.swing.text.AbstractDocument.fireInsertUpdate(Unknown Source)
at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
at javax.swing.text.AbstractDocument.insertString(Unknown Source)
at javax.swing.text.PlainDocument.insertString(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.setText(Unknown Source)
at javax.swing.plaf.metal.MetalComboBoxEditor$1.setText(Unknown Source)
at javax.swing.plaf.basic.BasicComboBoxEditor.setItem(Unknown Source)
at javax.swing.JComboBox.configureEditor(Unknown Source)
at
javax.swing.plaf.basic.BasicComboBoxUI$ListDataHandler.contentsChanged(Unknown
Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)

when i call setSelectedIndex(0) on a combobox filled with some strings.

Does somebody know what it means ?

Carsten
Paul Lutus - 23 Sep 2004 16:53 GMT
Carsten Schäfer wrote:

> I sometimes see the following exception:
> java.lang.IllegalArgumentException: Invalid location

/ ...

> when i call setSelectedIndex(0) on a combobox filled with some strings.
>
> Does somebody know what it means ?

Yes, it means there is no item with an index of 0. Solution: test for the
presence of this indexed item before trying to select it.

Remember, in programming, zero is a valid number just like one or two, and
if you specify an index of zero, there must be an index-zero item present,
or you will get an error.

Signature

Paul Lutus
http://www.arachnoid.com

Carsten Sch?fer - 23 Sep 2004 17:00 GMT
>> I sometimes see the following exception:
>> java.lang.IllegalArgumentException: Invalid location
[quoted text clipped - 12 lines]
> two, and if you specify an index of zero, there must be an index-zero
> item present, or you will get an error.

I don't understand your answer.
I thought setSelectedIndex(0) selects the first item in a JComboBox.
If there are no items in the combobox i get: setSelectedIndex: 0 out of
bounds

How do i test the presence of the item at index 0 ?

Carsten
Thomas Fritsch - 23 Sep 2004 17:31 GMT
Carsten Schäfer schrieb:

>>Carsten Schäfer wrote:
>>
[quoted text clipped - 21 lines]
>
> How do i test the presence of the item at index 0 ?

probably like this:
  if (comboBox.getItemCount() > 0)
    comboBox.setSelectedIndex(0);

> Carsten
>
Signature

Thomas<dot>Fritsch<squiggle>ops<dot>de

Carsten Sch?fer - 23 Sep 2004 17:16 GMT
> Carsten Sch?fer schrieb:
>>
[quoted text clipped - 25 lines]
>    if (comboBox.getItemCount() > 0)
>      comboBox.setSelectedIndex(0);

This tests if there are items in the combobox.
As i wrote i get 'java.lang.IllegalArgumentException: Invalid location'
and not 'setSelectedIndex: 0 out of bounds'
This would happen if there are no items in the combobox.
Or am i wrong ?

Carsten
Paul Lutus - 23 Sep 2004 17:47 GMT
Carsten Schäfer wrote:

/ ...

>> probably like this:
>>    if (comboBox.getItemCount() > 0)
[quoted text clipped - 5 lines]
> This would happen if there are no items in the combobox.
> Or am i wrong ?

Why not take the suggestion and test for the presence of an item at index
zero? See if this solves the problem.

Also, it is possible to put an "object" equal to null at index zero. This
might result in the error message you saw. We really cannot say without
seeing your code.

Signature

Paul Lutus
http://www.arachnoid.com

Babu Kalakrishnan - 24 Sep 2004 06:31 GMT
> Carsten Schäfer wrote:
>
[quoted text clipped - 16 lines]
> might result in the error message you saw. We really cannot say without
> seeing your code.

It would solve the problem only if it were an ArrayOutOfBoundsException
that he got.

If you looked at the stacktrace carefully, you would notice that the
exception was generated when swing tried to update the Caret in the
textbox portion - so there is no point trying to solve a non-existent issue.

To the OP : Most errors of this type occur if the thread safety rules of
the swing framework are not followed. So check your code to see if you
are making this call from some thread other than the EDT. (it certainly
appears to be that if what you posted is the *complete* stacktrace.
Stacktraces originating from code running in the EDT should have some
event queue processing code as the last lines)

BK
Thomas Fritsch - 23 Sep 2004 18:33 GMT
Carsten Schäfer schrieb:

>>Carsten Schäfer schrieb:
>>
[quoted text clipped - 35 lines]
>
> Carsten

Hmpf, you are right!
After re-reading the exception stack trace in your original post I saw:
The exception did *not* occur because 0 were an illegal combobox item
index, but because there was an illegal *character* index in the string
when JTextComponent#getSelectedText fiddled around with selected text
and caret position.
=> it may be a bug of yours or of Sun...
Consider posting a stripped down version of your source.

Signature

Thomas<dot>Fritsch<squiggle>ops<dot>de



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.