Daron
Daron

Reputation: 329

Error reading information from a JTable empty string error

Does the following code make sense? I'm basically trying to read in values from a JTable and cast them to double. But I'm getting the following errors. Specifically the error in the line 1567 that reads percentageValueDouble=Double.parseDouble(percentageValueString);
But I don't understand what the errors mean or how to fix them.

This is the source code:

if((gradesTable.getModel().getValueAt(i, j)!=null)&&(gradesTable.getModel().getValueAt(i, j)!="")){
    percentageValueObject=gradesTable.getModel().getValueAt(i, j);
    percentageValueString=percentageValueObject.toString();
    percentageValueDouble=Double.parseDouble(percentageValueString);
    }
    else
        percentageValueDouble=(Double) null;

And this is the error given:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at Gradebook$13.mousePressed(Gradebook.java:1567)

Upvotes: 1

Views: 242

Answers (1)

Nabin
Nabin

Reputation: 11776

So the value got is not Double for sure.

Reason

You definitely don't have a double in the cell.

You may have something like 50%, 10.5% or something similar.

What you need is 50 , 10.5. Just exclude '%'.

P.S. And certainly the reason is

You should use equals() method to check for empty string.

!gradesTable.getModel().getValueAt(i, j).equals("")

Or alternatively you can check for length as follow:

gradesTable.getModel().getValueAt(i, j).length()>0

Edit:

You are checking for empty string using == so the if block is running even if the string is empty. So the problem will be solved using equals() method.

Upvotes: 1

Related Questions