This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Counting occurrence of an item in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Counting occurrence of an item in JTable" Watch "Counting occurrence of an item in JTable" New topic
Author

Counting occurrence of an item in JTable

Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I have a simple JTable with 6 rows and 3 columns. What i want to count is how many people, thier favourite sport is, for example "Handball", or how many peoples name is "Radek".
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
In your example the data is all stored in an array of arrays so just iterate over the appropriate column in that and count the occurrences.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I achieve this by adding the code below.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38046
    
  22
I suggest you use the sizes of the data array in the loop, in case that somehow is different from column count and row count.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
After some changes in the code, i get java.lang.NullPointerException when i try to find matches after adding a couple of new rows. I guess this is because of "Object[][] data" has no rows, but as i mentioned i add some rows to JTable before trying to count occurences. Somebody now how to eliminate this error, because i don't have an idea.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
After some changes in the code, i get java.lang.NullPointerException when i try to find matches after adding a couple of new rows. I guess this is because of "Object[][] data" has no rows, but as i mentioned i add some rows to JTable before trying to count occurences.

Your analysis of the problem is correct, unfortunately you didn't mention you would be adding new rows in your original post and our advice so far has not allowed for this fact.
I suggest to change your nested loop back to using the column and row count and rather than using the data array and call the tables getValueAt() method to get the displayed data
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I changed back nested loop and i used the "getValueAt()" method to return cell value, but error stays the same.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
Please post the code and the full error message and stack trace.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
Here is the full error message and stack trace, the code is fifth post.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
Please don't go back and structurally change earlier posts after they have been replied to as it can make the replies meaningless and thus can make it hard for other people to understand the thread.

When faced with problems like this try adding a print statement before the line throwing the exception and print out the variables ie row and col number and the value returned by myJTable.getValueAt(row, col) to see what values are actually being used.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
Tony Docherty wrote:
Please don't go back and structurally change earlier posts after they have been replied to as it can make the replies meaningless and thus can make it hard for other people to understand the thread.

I will keep this in mind.
I added a "System.out.println(myJTable.getValueAt(row, col))" before the line throwing the exception and it printed single "null", i want to mention that i added some rows earlier to my JTable using JButton.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
And how exactly did you add these rows and what if any data was put into each cell in the row.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
Thats how i added that line. If no data is added to table it prints "0".
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
That's the code to count the occurrences not the code to add a row
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I don't changed the code to add a row, because error appears in the code to count the occurences and you wrote to try adding a print statement before the line throwing the exception.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
Radek Gajdos wrote:I don't changed the code to add a row, because error appears in the code to count the occurences and you wrote to try adding a print statement before the line throwing the exception.

Ok but then you said you'd added some rows by pressing a button and I asked you how you added the row and what data did you put into each cell in the row.

It's difficult trying to solve a problem when you haven't fully described what your code is doing and you haven't shown all the code. Can you provide an SSCCE so we can run it and see for ourselves what is happening.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I'm sorry for my absence, i had some work to do. First of all, i must apologize you for my behaviour, because of my ignorance i didn't notice that the code is correct and everything works perfectly unless every cell is filled (in my ignorance i run the code to count occurrences with table not fully filled). Second, there was some misunderstanding on which a take a blame, because of my english, i wrongly understood your replays. Third and last, i have another problem after adding new code responsible for filtering the table. Error occurrs when i try to count occurrences after filtering the table, in other words i filter table first and then i try to count occurrences.
Here the code:

And here is the full error message and stack trace:
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
If you have filtered the table then there is a likelihood that not all the table rows are visible. Your code has a take account of this so rather than calling the model's getRowCount() which returns the number of rows in the model you need to call the filters getViewRowCount() which returns the number of rows which are currently displayed. Then because the display index may no longer be the same as the model index (due to hidden rows), rather than passing the row number to getValueAt(..) you need to convert it from the view index to the model index via the table's convertRowIndexToModel() method.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
I changed part of my code to the one below and it works perfectly. Thank you for your advices. Peace!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
I'm surprised that works as you haven't converted the display row index to the model index. Are you sure you have tested your code properly?
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
If adding rows, sorting columns, filtering table and counting occurrences means properly then i have tested my code. If not please tell me how can i test my code diffrently.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
Have you tested all possible scenarios.
For example: When you filter rows and some rows disappear the effects that has on the output depends on which rows disappear and what data the following rows have. It is possible that for a given data set a row could be filtered out and your code looks at some of the wrong row indices but still counts the right number of occurrences. You need to make sure your data is organised in such a way that when you apply filter X there is 1 row of data that needs to be counted after that filtered out row. Furthermore you should run tests that put that row as the very next row to the row being filtered at, as the last row in the data set and then as a mid point between the two previous tests. Note: This is 3 separate tests.
Radek Gajdos
Ranch Hand

Joined: Sep 22, 2013
Posts: 32
Thank you Tony for your advices, i was using all my imagination to test my code and still i noticed no errors throving by my code, so until now i must write that it is working, maybe it isn't state of art but works fine for me, maybe in the near future problems occurrs when i be trying to implement new code to the existing now, but for now i write one more time that's working well.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Counting occurrence of an item in JTable
 
Similar Threads
JTable
JTable in JFrame
JTable
Consuming MouseEvent in JTable
JTable