File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes  java.lang.NumberFormatException when sorting table column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark " java.lang.NumberFormatException when sorting table column" Watch " java.lang.NumberFormatException when sorting table column" New topic
Author

java.lang.NumberFormatException when sorting table column

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm quite amazed at this exception because:
i used the same code in other applications
in this very application - and in the same jTable - all other columns sort ok, either being numeric or string values

the table is populated ok, the data displays ok, every other column has the sort ok, except for this column

this column is similar to others i have in tableDatabase . varchar[45]
the value is inputed as String

i cant understand

in the images i show bellow, the two columns are String/varchar[45]
i can sort column data perfectly, but when i try to sort column nome i get that exception

if you want i can post any relevant code

thanks in advance

EDIT i thought i could post several images but i realized i cant

the one shown here is when i get the NFE

i'll post the other images as answers to this post


[Thumbnail for trying to sort column Nome.PNG]



java amateur
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
this image refers to when the JTable gets populated:


[Thumbnail for initial display.PNG]

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
this image is when i sort column Data:


[Thumbnail for sorting column Data.PNG]

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
this image is when i sort - again - column Data:


[Thumbnail for again sorting column Data.PNG]

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i forgot:

where i get the NFE is in this class:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Your table data doesn't look like numbers, does it? So you can't use that comparator for sorting it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I don't understand how you expect characters "a, b, c, d..." to be parsed to an int?
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
maybe i'm wrong, but how do you explain that class helps sorting every other string column?

what i mean is it always helped me sorting strings, as in this very case, with other string columns (as shown in my images) ...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
maybe i'm wrong, but how do you explain that class helps sorting every other string column?


I doubt it.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
maybe i'm wrong, but how do you explain that class helps sorting every other string column?


I doubt it.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.


the code to run needed a database, i guess
anyway i posted some images that prove every other string column is correctly sorted...

(i'll be right back)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
the code to run needed a database, i guess


You don't need a database, you hardcode the data for a SSCCE.

anyway i posted some images that prove every other string column is correctly sorted...


An image proves nothing. It doesn't show the code you used to sort the table or the Comparator that was used.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i can post more relevant code if you wich, but i guess i cant write a new application to post here to examplify the error

i can post more images, if you like

the code i use in this method that initializes the JTable:


my tableModel:
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Its NOT a SSCCE and I'm not interested in looking at it.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
my last post was published before i read your last post



in first place i want to thank everyone who tries to help me

in the meantime i found that this strange behaviour doesnt depend on the data i've in it, but in the order i display the data: it always happens in my second (from left) jtable column

i believe the init code and the tablemodel are well written

as you said, the problem might be with my comparator (not designed for strings)

anyway any comment about the code i posted would be wellcome since would point to further clues (as you did with comparator)

thanks again
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:Its NOT a SSCCE and I'm not interested in looking at it.


i'm sorry to know that - i see this forum is not anymore as friendly as it used to be... but you are free to stand to your atitude
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
i see this forum is not anymore as friendly as it used to be...


I agree, people expect to be spoon fed the answer all the time and aren't willing to do some work on their own.

They give us half the information we need to solve the problem and expect us to be mind readers to understand what they are really doing.

I get tired of having to waste time by repeating a suggestion multiple times (not only in this posting, but in other postings as well).

Good luck with future questions.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
i see this forum is not anymore as friendly as it used to be...


I agree, people expect to be spoon fed the answer all the time and aren't willing to do some work on their own.

They give us half the information we need to solve the problem and expect us to be mind readers to understand what they are really doing.

I get tired of having to waste time by repeating a suggestion multiple times (not only in this posting, but in other postings as well).

Good luck with future questions.


i think my post was not like: "gimme some code to read a file, urgent, plzzz"

when i refer to friendly place, i recall that often times i was given an hand in a side matter in order to solve the concret problem, but from you i got the words: "I'm not interested in looking at it"

i posted the relevant code in order to be helped, not to be spooned

so i can state i dint expect to be spoon fed

isnt true i'm not willing to do some work on my own (i supose you read my pm)

i believe the relevant code i posted (which you were'nt interested in) had near 100% the information needed

as i already stated: i dont know how to hardcode data to display in the table, i'm sorry and yes, i believe one gets tired of repeating things over and over

i, as a teacher, i'm fully aware of that , but that's the life i chose
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
i posted the relevant code in order to be helped


You where asked (multiple times) for a SSCCE. When you have a problem YOU don't know what is or isn't relevant. Is the problem with your TableModel, the Comparator or something else. You don't know until the problem is solved. So your first step is to create a SSCCE that uses the DefaultTableModel and the default Comparators. The code will either work or it won't. If it works, then you know the problem is either the TableModel or the Comparator, so you change the code to try one or the other. Again it will either work or it won't. Once you get to a point when it no longer works you post the SSCCE stating "it worked when I did this, but stopped working when I did this". That is how you learn to debug. And that is why a SSCCE is important. Your problem would have been solved hours ago it you did this.

i dont know how to hardcode data to display in the table,


So you haven't read the JTable API, which has a link to the Swing tutorial on "How to Use Tables", which has working examples that all use hard coded data?

The tutorial even shows how to sort tables. I still have no idea why you think you need to use a custom comparator that is used to sort Integers, when your columns contain String data. Why do you add those lines of code to your program if you don't know what they do?
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:
i posted the relevant code in order to be helped


You where asked (multiple times) for a SSCCE. When you have a problem YOU don't know what is or isn't relevant. Is the problem with your TableModel, the Comparator or something else. You don't know until the problem is solved. So your first step is to create a SSCCE that uses the DefaultTableModel and the default Comparators. The code will either work or it won't. If it works, then you know the problem is either the TableModel or the Comparator, so you change the code to try one or the other. Again it will either work or it won't. Once you get to a point when it no longer works you post the SSCCE stating "it worked when I did this, but stopped working when I did this". That is how you learn to debug. And that is why a SSCCE is important. Your problem would have been solved hours ago it you did this.

i dont know how to hardcode data to display in the table,


So you haven't read the JTable API, which has a link to the Swing tutorial on "How to Use Tables", which has working examples that all use hard coded data?

The tutorial even shows how to sort tables. I still have no idea why you think you need to use a custom comparator that is used to sort Integers, when your columns contain String data. Why do you add those lines of code to your program if you don't know what they do?


i do really apreciate your answer - thank you

i smyled at certain points, as for example when you explain how to debug, because i simply debug using print lines to check what is wrong... but for sure you'r right

as i previously said, i use code i had used in other applications i had build - and maybe then i needed to sort numeric data as well and didnt recall that, because it sorted string data too, i can assure

you made a point - i'm gonna reread the sun java tutorial on jtable, but not tonight (its past 3 in the morning here )

as soon as i have a new understanding of my problem i'll come here

thank you

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
here's what i got:

for the same data, my comparator sorts either numeric or string data correctly or not (NFE on 2nd column) depending on the order type columns appear



i offer two cases:

using Object[][] data with String[] columnNames i get all columns perfectly sortable

using Object[][] data2 with String[] columnNames2 i get all columns perfectly sortable except for dreaded 2nd column



so, what i take from here?

probably my comparator isnt apropriated, but the question remains: why sometimes it does work properly and others dont?

more: as a (somewhat poor) workaround, i can move string column from 2nd place, and then i get all data sorted finely

(i also noticed "minimal" string sorter used in sun java tutorial on jTable doesnt sort numeric data, as expected)

here's the (working) code:
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
but the question remains: why sometimes it does work properly and others dont?


I don't understand your confusion. Twice you have already been told that not all Strings can be converted to Integers.

The String:

"2001" can be converted to an integer.
"Citizen Kane" can not be converted to an integer.

Therefore you can't use your Comparator unless all the values in the column can be converted to an integer. I don't understand why you would even attempt to use an Integer Comparator on this column?

Finally, there is no need for a custom Comparator. The sort is smart enough to choose an appropriate Comparator for the data in the column. All you need to do is tell the table model what kind of data if found in each column. Here is some general code to do this. (Again the basics of this code where taken from the Swing tutorial):

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks a lot, Rob

i'm a fervorous adept of the socratic method: guiding the pupil in a way he will be the one to find the answer

when reading your reply i said to myself eureka when you refered the comparator used on this column

so i rushed to comment that line:


and all is really fine now

i violated one of the capital laws of code: commenting for later understanding, so actually i just copy/paste without even having a clear idea of what exactly i was trying to achieve in this concrete case

you gave me back my weekend with wife and kids



have a nice weekend
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.NumberFormatException when sorting table column