• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

TableModel

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Voila
 
The only cure for that is hours of television radiation. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic