Results 1 to 8 of 8
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default AWT-EventQueue-0 NullPointerException

    I am receiving this error whenever my CellRenderer reaches a blank cell in my table. I tried using the if value == null return but it didn't help at all. Now, the reason I believe this is due to the blank cell is because my println, which is currently commented out, printed at row 18 just before I got the error. Row 18 is the first instance in which the value was blank. Also, if I get the value from any column that doesn't have blanks, it works fine.

    Does anyone have any ideas I could try?

    Here is my "sub class"
    Java Code:
    	class ColorRenderer extends DefaultTableCellRenderer
    	{
    		public Component getTableCellRendererComponent(JTable table, Object value, 
    				boolean isSelected, boolean hasFocus, int row, int column) {
    			
    			if (value == null)
    				return this;
    			//System.out.println("value is " + value + " at row " + row + " and column " + column);
    			Component cmp = super.getTableCellRendererComponent(table, value, 
    					isSelected, hasFocus, row, column);
    			String strFamilyValue = table.getModel().getValueAt(row, 38).toString();
    			//System.out.println("Value is " + table.getModel().getColumnName(38));
    			if (strFamilyValue != null && !strFamilyValue.equals("")) {
    				cmp.setBackground(Color.red);
    			}
    			else {
    				cmp.setBackground(null);
    			}
     
    			return cmp;
    		}
    	} // End sub class
    Here is the stack trace
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at MyJTable$ColorRenderer.getTableCellRendererComponent(MyJTable.java:78)
    	at javax.swing.JTable.prepareRenderer(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
    	at javax.swing.plaf.ComponentUI.update(Unknown Source)
    	at javax.swing.JComponent.paintComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent.paintForceDoubleBuffered(Unknown Source)
    	at javax.swing.JViewport.blitDoubleBuffered(Unknown Source)
    	at javax.swing.JViewport.windowBlitPaint(Unknown Source)
    	at javax.swing.JViewport.setViewPosition(Unknown Source)
    	at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.vsbStateChanged(Unknown Source)
    	at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.stateChanged(Unknown Source)
    	at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source)
    	at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source)
    	at javax.swing.DefaultBoundedRangeModel.setValue(Unknown Source)
    	at javax.swing.JScrollBar.setValue(Unknown Source)
    	at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.mouseWheelMoved(Unknown Source)
    	at java.awt.Component.processMouseWheelEvent(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.dispatchEventToSelf(Unknown Source)
    	at java.awt.Component.dispatchMouseWheelToAncestor(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)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Which line is line #78?

    kind regards,

    Jos

  3. #3
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Line 78 is...
    String strFamilyValue = table.getModel().getValueAt(row, 38).toString();

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ribbs2521 View Post
    Line 78 is...
    String strFamilyValue = table.getModel().getValueAt(row, 38).toString();
    You table and model aren't null so most likely the value in the table at position row, 38 is null.

    kind regards,

    Jos

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Set your debugger to stop at this line and check what item is null. This one line alone can have a null table, model, value, or row. Once you figure out what's causing the NPE we can help fix it.

    [Edit] Jos beat me to it.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You table and model aren't null so most likely the value in the table at position row, 38 is null.
    Jos
    That is what I thought it was, do you know why my if statement isn't returning then?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ribbs2521 View Post
    That is what I thought it was, do you know why my if statement isn't returning then?
    The value of the current cell (to be rendered) sure isn't null, you can trust that but the cell at that other location can be null (as you must've noticed by the stack trace of that thrown Exception).

    kind regards,

    Jos

  8. #8
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    OK, I see what you mean, the value of the cell renderer is not null but the value in the cell I'm evaluating can be. Which it is and that's why I'm getting this.

    Maybe I'm in over my head here but I tried an attempt at handling it by returning if the getValueAt came back null but now what happens is the renderer carries over the values from the previous column if the next column is blank. I guess my biggest problem here is I don't know exactly what the renderer does and I can't find anything online that really explains the details.

    I don't understand what I should be doing to handle these blank cells in the columns. I can't find anything on how renderers work, all I can find are examples and I haven't found any examples that show how to handle null values. Is there any info somewhere that explains in detail what the renderer is actually doing?

    EDIT: I've been playing around for a bit now and I think I finally have it. I was able to handle the exception if the value in column 38 was null but then it was still carrying values from previous columns into the blanks values of the following columns. So I removed the If (value == null) and it seems to be working. I guess since it was just returning when the value was null, the value was being replaced with what was left for that position in the previous renderer. Again, I don't really know the details of how they work but that's what I think happened. Also, I would still like to know if anyone is aware of a site I could find to explain how renderers work.
    Last edited by ribbs2521; 11-04-2009 at 12:08 AM.

Similar Threads

  1. Replies: 5
    Last Post: 08-26-2009, 12:37 PM
  2. Replies: 6
    Last Post: 08-03-2009, 09:58 PM
  3. Replies: 12
    Last Post: 10-05-2008, 07:52 AM
  4. Replies: 3
    Last Post: 01-29-2008, 02:37 AM
  5. Replies: 2
    Last Post: 07-16-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •