I have accepted various types of data inputs (integer, double, string and date) to my embedded JDBC database via (JTextLabels EDIT: JTextFields) using ResultSet and i am displaying results in a swing JTable using my custom AbstractTableModel. The following are snippets from my codes where am thinking the problem might lie, but i’m not sure;
public void connectToDB()
{
try
{
connection = DriverManager.getConnection(DBURL, DBUSERNAME, DBPASSWORD);
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
connectedToDB = true;
}
catch (SQLException sqlEx)
{
sqlEx.printStackTrace();
}
}
//Overrides AbstractTableModel's getValueAt() method
public Object getValueAt(int row, int column) throws IllegalStateException
{
connectToDB();
if (connectedToDB == false)
throw new IllegalStateException ("Not Connected To database");
else
{
try
{
if ( resultSet.next() == true )
{
resultSet.absolute( row + 1 );
return resultSet.getObject( column + 1 );
}
}
catch (SQLException sqlEx)
{
sqlEx.printStackTrace();
}
}
return "";// returns an empty string Object in case an error occurs above
}
It looks as if somwhere around the getValueAt() method, it generates the following exception, but even the stacktrace doesnt point me to any part of my code;
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java:487)
at java.text.Format.format(Format.java:140)
at javax.swing.JTable$DoubleRenderer.setValue(JTable.java:5315)
at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(DefaultTableCellRenderer.java:223)
at javax.swing.JTable.prepareRenderer(JTable.java:5683)
at javax.swing.JTable.getToolTipText(JTable.java:3328)
............
*Others ommitted 4 brevity*
............
I dont know why.
The stacktrace indicates the problem is situated in the renderer of the table. The
JTablehas decided it will use theDoubleRendererfor that particular value, and that value can not be parsed to aDouble. I would suggest to check yourTableModel#getColumnClassimplementation and see whether it returnsDouble.classfor a column which actually does not containDoubles since that is the method which is used to determine which renderer to use.This can be seen in the source code for
JTable, more specifically in the source of thegetRenderermethod. See below for the relevant part (line 3454 in the source code I linked to)