• Post Reply Bookmark Topic Watch Topic
  • New Topic

can you see how to handle this exception in following program  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.List;

import java.sql.*;
import java.net.*;

import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JTable;
public class Jtabinsertion {
public static void main(String[] args){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc racle:thin:@172.16.40.4:1521:hpsipos","commorm","commorm");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from pty_im where rownum<3");
JTable jtb=null;
Vector columnnames=new Vector();
columnnames.addElement("pty_cd");
columnnames.addElement("csnr_flg");
Vector rownames=new Vector();
Vector rowdata=new Vector();
//ArrayList rowdata=new ArrayList();
int i=0;
int pre=0,post=0;


while(rs.next()){
if(i<3)
{
rownames.addElement(rs.getString("pty_cd"));
rownames.addElement(rs.getString("csnr_flg"));
post=rownames.lastIndexOf(rownames.lastElement())+1;
System.out.println("pre:"+pre);
System.out.println("post:"+post);
rowdata.add(rownames.subList(pre,post));
//System.out.println(rownames.toString());
pre=post;
i++;
}
}




//System.out.println(rowdata.toString());
jtb=new JTable(rowdata,columnnames);
JFrame jf=new JFrame();
jf.add(jtb);
jf.pack();
jf.setVisible(true);
/*System.out.println(rs.getString("pty_cd"));
System.out.println(rs.getString("csnr_flg"));
System.out.println(rownames.elementAt(1));*/
// System.out.println(rowdata.toString());
}



catch(Exception e){
System.out.println(e);
}
}
}
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags so that the forum can format and display your source code properly.

What exception are you getting? Please copy and paste the exception message here. We can't help you if you don't explain exactly what the problem is.
[ August 21, 2007: Message edited by: Jesper Young ]
 
sampath kumar yanagandla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply
i got
"java.lang.ClassCastException: java.util.Collections$SynchronizedRandomAccessList"
exception
don't know where exactly it's wrong.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change your catch clause to include the statement



the resultant output will tell you exactly where the problem is occuring.
 
sampath kumar yanagandla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the exception tree

"java.lang.ClassCastException: java.util.Collections$SynchronizedRandomAccessList
at javax.swing.table.DefaultTableModel.justifyRows(DefaultTableModel.java:242)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:198)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:133)
at javax.swing.JTable.<init>(JTable.java:451)
at test.Jtabinsertion.main(Jtabinsertion.java:50)"

but why it is giving error at JTable constructor???
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the documentation, the first argument to the JTable constructor must be a Vector of Vectors. In your code that's not the case. The bug is in this line:

rowdata.add(rownames.subList(pre,post));

Note that subList(...) does not return a Vector - it returns a List. So you don't have a Vector of Vectors, but a Vector of Lists - which is not what the JTable constructor expects.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!