Anybody know what the heck is IllegalArgumentException..

Hi,

Anybody know what exactly means by IllegalArgumentAcception? In my program the error message print out like this:

java.lang.IllegalArgumentException: bad position: 1at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1650)

From my coding:

try

{

String val ="A";

int pos = getCaretPosition();

getDocument().insertString(getCaretPosition(),val,null);

setCaretPosition(pos+1);

moveCaretPosition(pos+1);

requestFocusInWindow();

}catch(Exception input)

{ input.printStackTrace();}

It seems that the setCaretPosition(pos+1)

in my coding facing problem. But I sure that the coding correct. Is there something out of my sight or something I incorrectly put in there. I need opinion on this matter. Can anybody help?

thanks

[1106 byte] By [eastern_paladina] at [2007-11-26 15:58:26]
# 1
If you look it up in the javadocs, it will tell you exactly what it means[url] http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)[/url]
macrules2a at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
Ok i have look through it. So what should i do? It say that the value position in setCaretPosition(Int position) is less that zero or greater that the component text length. That why its IllegalArgumentException
eastern_paladina at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 3
pos + 1 must either be less than zero or greater than the length of your text
macrules2a at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 4
pos + 1 must either be less than zero or greater than the length of your textIt's 1, it says so in the exception message.
itchyscratchya at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 5
For the coding that i provide above, I have connected it with JTextField. Is it possible that the exception occured because the JTextField have nothing or there is no word or character in the JTextField.Thanks
eastern_paladina at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Is it possible that the exception occured because the JTextField have nothing or there is no word or character in the JTextField.

You tell us. You wrote the code. You are looking at the GUI. Its a simpler error to fix. Use:

textComponent.getDocument().getLength();

This will tell you how many characters are in the Document. You can only position the caret between 0 and the value from above.

camickra at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 7
the exception message occured when I run the coding.. Ok I'll try your solution.
eastern_paladina at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 8

Still don't understand. I've try to increase the position by adding like this:

int pos = getDocument().getLength() + 1;

.

Now the exception message stated like this:

java.lang.IllegalArgumentException: bad position: 2

at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1650)

Argggggh it give me headache...

eastern_paladina at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...
# 9

If you have a text component with the following text: "a", then you can set the caret position at either 0, 1 (before the "a" and after the "a")

If you have a text component with the following text: "ab", then you can set the caret position at either 0, 1, 2 (before the "a" and after the "a" and after the "b")

I don't understand your confusion. You can only set the caret position before and existing character or after the last character.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 22:19:30 > top of Java-index,Desktop,Core GUI APIs...