Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4819544

SwingSet2 JTable Demo throws NullPointerException

XMLWordPrintable

    • b10
    • generic, x86, sparc
    • generic, solaris, solaris_9, windows_xp



        Name: dk106046 Date: 02/17/2003

        In the JTable demo, if I double click on the first cell in the Favorite Number column, delete the value and click on some
        other cell, a java.lang.NullPointerException is getting thrown.
        *****************************************************************************************************************************

        java.lang.NullPointerException
                at TableDemo$10.getColumnClass(TableDemo.java:498)
                at javax.swing.JTable.getColumnClass(JTable.java:1762)
                at javax.swing.JTable.getCellRenderer(JTable.java:3683)
                at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1177)

                at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1080
        )
                at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1003)
                at javax.swing.plaf.ComponentUI.update(ComponentUI.java:162)
                at javax.swing.JComponent.paintComponent(JComponent.java:566)
                at javax.swing.JComponent.paint(JComponent.java:833)
                at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4885)

                at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4838)
                at javax.swing.JComponent._paintImmediately(JComponent.java:4782)
                at javax.swing.JComponent.paintImmediately(JComponent.java:4591)
                at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:430)

                at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
        EventQueueUtilities.java:137)
                at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:204)
                at java.awt.EventQueue.dispatchEvent(EventQueue.java:526)
                at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
        read.java:237)
                at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
        ad.java:181)
                at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:175)

                at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:167)

                at java.awt.EventDispatchThread.run(EventDispatchThread.java:130)
        *****************************************************************************************************************************
         
        Expected Result
        The exception should not get thrown.

        Steps to Recreate:
        1) cd to \Java141\demo\jfc\SwingSet2
        2) enter appletviewer SwingSet2.html
        3) Select the JTable demo
        4) Double click on the first cell in the Favorite Number column. Delete the value. Click on another cell.

        For empty cells, getValueAt(row, col) method returns null and hence getColumnClass() method
        in TableDemo throws NPE.
        I can think of two fixes for this bug.

        1) ----------------------

            public Class getColumnClass(int c)
            {
                Object obj = null;
                for (int i=0; i<getRowCount(); i++)
                {
                    obj=getValueAt(i, c);
                    if(obj!=null) return obj.getClass();
                }
                return Object.class;
            }
        ------------------------

        2)---------------------
            public Class getColumnClass(int c)
            {
                return c==4 ? Double.class : getValueAt(0, c).getClass();
            }
        -----------------------
        I am going for fix 2, as 5th column (ie. c=4) will be always Double.

        ======================================================================

              psadhukhan Prasanta Sadhukhan
              dkorbel David Korbel (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: