• Post Reply Bookmark Topic Watch Topic
  • New Topic

TableModel  RSS feed

 
xin hao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I created a TableModel: ResultSetTableModel extends AbstractTableModel
the ResultSetTableModel use a Resultset in construct such as
public ResultSetTableModel(ResultSet re) {
this.resultset=re;
}
ResultSetTableModel's method getRowCount() is
public int getRowCount() {
try{
resultset.last();
return resultset.getRow();
}
catch(SQLException e)
{
e.printStackTrace();
return 0;
}
}
it compiled well but when i run it i get

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.xinhao.swing.ResultSetTableModel.getColumnCount(ResultSetTableModel.java:49)

How can I correct it?
the java version is jdk1.6
thanks!
 
Romeo Son
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xin,

You posted the implementation of getRowCount method but the exception is in the implementation of the getColumnCount method.

Try to debug, put a breakpoint in that method and you will figure out which object is null. You are trying to do a method call on a null object in getColumnCount implementation.

Hope this helps,

Romeo
 
xin hao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much.

I changed nothing but add a JDBCConnection in the ResultSetTableModel such as followed:

public class ResultSetTableModel extends AbstractTableModel{
ResultSet resultset;
public ResultSetTableModel(){

Class.forName(driver).newInstance();
Connection connection = DriverManager.getConnection(url,user,pass);
Statement statement = connection.createStatement();
this.resultset = statement.executeQuery("some sql query ");

}
public int getRowCount() {
try{
this.resultset.last();
return this.resultset.getRow();
}catch(SQLException ex){
ex.printStackTrace();
}
return 0;
}

}
.....
every thing run well but when i just send a ResultSet in this ResultSetTableModel it can not run.
 
Andrei Pat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTable has to know how many rows and how many columns there are in the model. You need to write an implementation of the getColumnCount method that returns how many columns the table has. Also, you need an implementation of getValueAt

 
xin hao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thinks Romeo Son ,thinks Andrei Pat .
I add "if (resultset!=null)" in my code ,it run well although there isn't any thing in my JTable.
Next i put a breakpoint in method and find the ResultSet i sent out is null.
I have corrected the wrong codes in geting results from database.
Now everything run well.
thanks very much.
 
Romeo Son
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Voila
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!