This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes String index out of bound exception in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "String index out of bound exception in JTable" Watch "String index out of bound exception in JTable" New topic
Author

String index out of bound exception in JTable

G Kiran Kumar
Greenhorn

Joined: Apr 12, 2009
Posts: 14
I used JTable to update records in a table. In the following code, I am getting
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
exception in the line :
table = new JTable(this);


However, this is happening ONLY for the table 'Triangle' and not for the other tables. What could be wrong ???


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Can you perhaps show the entire stack trace? There is something in the table model, but without that code or the stack trace it's impossible to know what.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
G Kiran Kumar
Greenhorn

Joined: Apr 12, 2009
Posts: 14
This is the only line that is shown as error while executing that class file. :-(
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

But without the stack trace we still can't see what exactly is going wrong. There are quite a lot of method calls from the JTable constructors that it's hard to see what exactly goes wrong just by looking at this code.
G Kiran Kumar
Greenhorn

Joined: Apr 12, 2009
Posts: 14
This is the complete code of the .java file. I have a table Triangle in Microsoft access DB. It has 14 columns


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the culprit is likely to be in this bit



in particular these 2 lines
pos = colname.indexOf("_");
colname = colname.substring(0,pos) + " " + colname.substring(pos+1) ;

if there is not a '_' in colname, pos = -1, and you will get the exception you see.

try it this way, and see if it fixes the problem



G Kiran Kumar
Greenhorn

Joined: Apr 12, 2009
Posts: 14
Thanks a lot.
It is working now.
 
 
subject: String index out of bound exception in JTable
 
Similar Threads
jtable
JTable
JTable
JTable
JTable ........