Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NumberFormatException when sorting table column

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
trying to sort column Nome.PNG
[Thumbnail for trying to sort column Nome.PNG]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this image refers to when the JTable gets populated:
initial display.PNG
[Thumbnail for initial display.PNG]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this image is when i sort column Data:
sorting column Data.PNG
[Thumbnail for sorting column Data.PNG]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this image is when i sort - again - column Data:
again sorting column Data.PNG
[Thumbnail for again sorting column Data.PNG]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i forgot:

where i get the NFE is in this class:
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your table data doesn't look like numbers, does it? So you can't use that comparator for sorting it.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand how you expect characters "a, b, c, d..." to be parsed to an int?
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its NOT a SSCCE and I'm not interested in looking at it.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic