aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Getting Null Pointer Exception when getting the value after cell ediiting in Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting Null Pointer Exception when getting the value after cell ediiting in Jtable" Watch "Getting Null Pointer Exception when getting the value after cell ediiting in Jtable" New topic
Author

Getting Null Pointer Exception when getting the value after cell ediiting in Jtable

Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Hi all,

Can any one please help me in solving the error of Null Pointer Exception when getting the value of a cell after editing in Jtable


I am using the code as foolows





Can any one help me in "where I had commited wrong in my code",

and please suggest me to get rid of this error,


Thank you very much,


Jashvika
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
It would help if you posted a short piece of runnable code that demonstrated the problem, as well as posting the entire text of the Exception, pointing out any line numbers referenced in the stack trace.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
You where given the answer two days ago in your other posting.
Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Hello Rob Camick Sir,

I had included the stopcellEditing(), also

you can see that in line 77 in my code

But still I am getting the Null Pointer Exception

So for that I am asking where I had been mistaken because I am new to this topic,


Thnak you very much,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
1) I suggested you use the default editor, you are still using your custom editor

2) I suggested you use the getValueAt(...) method. You are still using your old code.

3) I gave you a link that showed a couple of different ways to stop editing of a cell. One solution was 1 line of code, the other was 2 lines of code. Instead you come up with nested for loops for some reason which looks nothing like the code I suggested.

You have not followed any of my suggestions so I'm not about to spend time debugging your code.
Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Hello Rob Camick Sir,

I don't have any idea about this cell editing,

I don't Know about Default cell editing and custum cell editing but I read now some examples and I found a piece of code like this,




Can you please suggest a link or any documentation to know the difference between custom cell editor and default cell editor,


If you help me I willl be very much thankful to you,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

I still see nothing Kevin or Rob C asked for. How about that stack trace, for instance?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Rob Prime Sir,

The Stack trace is as follows


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at client.cpm.dailyT8.alarm.CBRCPMDailyT8Update.updateButton_actionPerformed(CBRCPMDailyT8Update.java:121)
at client.cpm.dailyT8.alarm.CBRCPMDailyT8Update.access$0(CBRCPMDailyT8Update.java:102)
at client.cpm.dailyT8.alarm.CBRCPMDailyT8Update$1.actionPerformed(CBRCPMDailyT8Update.java:95)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)




please tell me sir how to use DefaultCell Editor for cell editing and how to get the value after editing,

It is very important for me or please suggest me any Links so that I can go on,

Thank you very much
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Kushi Gadiparthi wrote:
at client.cpm.dailyT8.alarm.CBRCPMDailyT8Update.updateButton_actionPerformed(CBRCPMDailyT8Update.java:121)

So there you have the exact line where the error occurs: 121. If we analyze the method in which it occurs (updateButton_actionPerformed), there are a few possibilities (mentioned in comments):
Why I think that this.getTable().getCellEditor() returns null? Because that's what the API says:
Returns:
the TableCellEditor that does the editing, or null if the table is not currently editing.

You loop through all cells. If the cell is editable (not being edited, being editable; big difference!) you get the single available cell editor and tell it to stop editing. Unless this fails that means that after the first call the table is not currently editing any more. Any subsequent call to this.getTable().getCellEditor() will return null. And since you are looping through all cells, there is bound to be another editable cell and you call the method again.

Replace your entire loop with a simple if statement:
Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Hello Rob Prime Sir,

Thank you very much for your help

I had replaced my hole loop like this



But now I am not getting the error and I am not getting the edited value also,

Help me in this,

Thank you very much
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

That's because editor is a new table cell editor that's not attached to the table in any way. You'll need to use the old editor:
This is just basic code, without proper handling of a null editor or a null editor value.
Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Hello Rob Prime Sir,

I did not understand this logic,

But where we giving reference to MyTableCellEditor class

Can you please explian me,


Thank you very much,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You'll need to set it for the table, or table column. Check out the APIs of JTable and TableColumn. Inside your action listener you don't need a reference to it anymore.
Kushi Gadiparthi
Ranch Hand

Joined: Sep 23, 2010
Posts: 52
Thank you very much Sir,


thank you very much for your help,

Today I had came to know some new things with your help,


My Editing had worked perfectly,


Thanks a lot,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You're welcome.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
The code may work, but as far as I'm concerned the code is WRONG. There is no need to reference the actual cell editor to get the data. When the cell is finished editing the editor will automatically save the data to the TableModel, which is why I suggested long ago to use the getValueAt(...) method.

There is still no need to write a custom editor either. The JTable will use the default edtior by default.

All you have to do is read the JTable API and you will find a link to the Swing tutorial on "How to Use Tables" Which has example code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Null Pointer Exception when getting the value after cell ediiting in Jtable