Reputation: 33
I have a H2 database, and I want to read records from a JTable
to populate the database tables. However my table can have empty records.
I'm using this method:
String descr = jTable2.getValueAt(i, 1).toString();
And I'm getting this error each time a field in JTable
(i, 1) is empty:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException.
Any suggestions how to read and store empty values from table without this error?
EDIT: For more explicit example, anytime i do jTable2.getValueAt(i, 1) in a empty field, same null exception occurs..
for (int i = 0; i <10; i++) {
int processos = Integer.parseInt(jTable2.getValueAt(i, 0).toString());
String descr = jTable2.getValueAt(i, 1).toString();
if(!descr.isEmpty())
descr = jTable2.getValueAt(i, 1).toString();
else{
descr= "";
}
//String data = jTable2.getValueAt(i, 2).toString();
System.out.println(processos + descr);
try {
databaseManager.saveTable(intIDinternto, processos, descr, "aaaa");
} catch (SQLException ex) {
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
}
SOLUTION: Use object first, then parse to string and compare.
for (int i = 0; i <10; i++) {
String description = " ";
int processos = Integer.parseInt(jTable2.getValueAt(i, 0).toString());
Object descr = jTable2.getModel().getValueAt(i, 1);
if (descr!= null)
description = jTable2.getModel().getValueAt(i, 1).toString();
else
description = ".";
//String data = jTable2.getValueAt(i, 2).toString();
System.out.println(processos + description);
try {
databaseManager.saveTable(intIDinternto, processos, description, "aaaa");
} catch (SQLException ex) {
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
}
Upvotes: 1
Views: 340
Reputation: 59950
You have to make a check for example :
String descr = jt.getValueAt(0, 1).toString();
int storedValue = 0;
//check if your value is a correct integer or not
if (descr.matches("\\d+")) {
storedValue = Integer.parseInt(descr);
} else {
System.out.println("Exception");
}
System.out.println(storedValue);
The idea : check your value is correct number or not using regex descr.matches("\\d+")
if the input is correct then you can parse it else you can't, you have to throw an exception or any thing else
Upvotes: 1