This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String index out of bound exception in JTable

 
G Kiran Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
G Kiran Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the only line that is shown as error while executing that class file. :-(
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
It is working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic