aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes DefaultTableModel got a NullPointerException. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "DefaultTableModel got a NullPointerException." Watch "DefaultTableModel got a NullPointerException." New topic
Author

DefaultTableModel got a NullPointerException.

Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
I am trying to show JProgressBar in JTable, but It got NullPointerException and IndexOutOfRangeException.so I am here to looking for some help.
thanks everyone.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
Welcome to the ranch.

Please also post the full error message and stack trace.

The NPE is probably caused because refreshPopup() calls hidePopupBars() which calls popup.hide() without checking to see if popup is non null. And a Popup object is only created before a refreshPopup() call if the user clicks on the task count label which seems a little strange.
Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
Tony Docherty wrote:Welcome to the ranch.

Please also post the full error message and stack trace.

The NPE is probably caused because refreshPopup() calls hidePopupBars() which calls popup.hide() without checking to see if popup is non null. And a Popup object is only created before a refreshPopup() call if the user clicks on the task count label which seems a little strange.

Thanks for your help. checking null is done before refreshPopup() has be called, but you are right, I move the checking stuff into hidePopupBars().
Create Popup object Frequently is because the Table's size is dynamic , for if add a new task when Popup window is visible, it should show the new added task ProgressBar.
Here is a stack trace.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
I'm still not 100% sure what you are trying to achieve but it looks like a lot of complexity for something that should be fairly simple.

Your exception is probably caused because you are changing the size of the model (ie adding/removing items) without ever firing the table updated event ie fireTableCellUpdated(row, col);
Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
Tony Docherty wrote:I'm still not 100% sure what you are trying to achieve but it looks like a lot of complexity for something that should be fairly simple.

Your exception is probably caused because you are changing the size of the model (ie adding/removing items) without ever firing the table updated event ie fireTableCellUpdated(row, col);

Thanks for your help. here is a simple version.


Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
Tony Docherty wrote:I'm still not 100% sure what you are trying to achieve but it looks like a lot of complexity for something that should be fairly simple.

Your exception is probably caused because you are changing the size of the model (ie adding/removing items) without ever firing the table updated event ie fireTableCellUpdated(row, col);

The API document says what fireTableCellUpdated(row, col) does is to notice Listener data has changed.but I have no Listener on Table.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
Just because you don't add a listener doesn't mean there aren't any listeners. JTable may well add listeners to its model.

Change your model as follows so data can't be null:
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
I've just noticed (not sure how I missed it before now) you are extending DefaultTableModel and providing your own data storage collection - why?
If any of the model's methods that you haven't overridden are called the results of the call will be relative to DefaultTableModel's data store and not yours.
Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
Tony Docherty wrote:Just because you don't add a listener doesn't mean there aren't any listeners. JTable may well add listeners to its model.

Change your model as follows so data can't be null:

Sorry, your solution are not correct. I just try to replaced DefaultTableModel by AbstractTableModel, and It worked.
Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
Tony Docherty wrote:I've just noticed (not sure how I missed it before now) you are extending DefaultTableModel and providing your own data storage collection - why?
If any of the model's methods that you haven't overridden are called the results of the call will be relative to DefaultTableModel's data store and not yours.

Thanks, I use AbstractTableMode instead. and I was wonder if DefaultTableModel(Vector data, Vector Columns) is better way.
The IndexOutOfBoundsException is because during Event Dispatch Thread is reading data from List Task Thread do changed the List(i.e: add(), remove()) at the same time. I put every Action which effected TableModel data in EDT. and no more IndexOutOfBoundException throwed.

m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
- do not add JComponents to the XxxTableModel, use primitive type and change only the value

- there are two ways by using Runnable#Thread or SwingWorker, Runnable#Thread is clear, simple and returns exceptions properly in compare with SwingWorker (get() in done())

- read Oracle JTable tutorial about Model and Renderer



two code examples about JTable and Renderer



and

Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
m Korbel wrote:- do not add JComponents to the XxxTableModel, use primitive type and change only the value

- there are two ways by using Runnable#Thread or SwingWorker, Runnable#Thread is clear, simple and returns exceptions properly in compare with SwingWorker (get() in done())

- read Oracle JTable tutorial about Model and Renderer



two code examples about JTable and Renderer


Thanks a lot. Jprogress in JTable should be written like below?
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
1. your AbstractTableModel is most complicated, remove useless data about Vector and casting (Vector) too


2. why complicating simple things, use implemented setValueAt instead





4. never to call fireXxxXxx(); outside model, your model contains setValueAt, and this notifier will be notify properly, remove this code line

5. if isn't there special reason use DefaultTableModel instead, all notifiers are implemented and correctly (see my code example)

6. did you run/debug my code examples, everything (about to change JProgressBar as Renderer) is about setValueAt

Leajon First
Greenhorn

Joined: Nov 21, 2012
Posts: 10
m Korbel wrote:1. your AbstractTableModel is most complicated, remove useless data about Vector and casting (Vector) too


2. why complicating simple things, use implemented setValueAt instead





4. never to call fireXxxXxx(); outside model, your model contains setValueAt, and this notifier will be notify properly, remove this code line

5. if isn't there special reason use DefaultTableModel instead, all notifiers are implemented and correctly (see my code example)

6. did you run/debug my code examples, everything (about to change JProgressBar as Renderer) is about setValueAt


Thank a lot, your words are very helpful for me.
I was read the API document of DefaultTableModel carefully on Sunday. It just as you said addRow(), removeRow(), setValueAt() do notifys. your demos are awesome.
http://java-swing-tips.blogspot.com/2008/03/jprogressbar-in-jtable-cell.html is a dead link, can you give some more demos about JTree, JCombox something.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DefaultTableModel got a NullPointerException.