Search code examples
javaswingnullpointerexceptionjtableuimanager

setLookAndFeel() gives NullPointerException if I use JTable


I have a JFrame with two panels. And I am setting the UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) in a class which extends JFrame.So It works fine for both panels If I dont've JTable in it. But If I add JTable to any panel then going to that panel gives me NullPointerException like below.

So what is the problem here, am I need to set anything for JTable also

EDIT: I figured it out that the problem is not with JTable but with tableCellRenderer. I am adding a tableRenderer like below

public class Frame extends JFrame
{
public void initialize()
{
    try
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
    {
        e.printStackTrace();
    }

    JPanel panel1 = new JPanel();
    panel1.add(new JLabel("Panel1"));
    JPanel panel2 = new JPanel();
    JTable table = new JTable(2,2);
    table.getColumnModel().getColumn(1).setCellRenderer(new CheckBoxRenderer()); // Adding this renderer creates me problem
    JScrollPane jScrollPane = new JScrollPane(table);
    jScrollPane.setSize(100, 100);
    panel2.add(jScrollPane);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Panel1", panel1);
    tabbedPane.addTab("Panel2", panel2);
    add(tabbedPane);
    setSize(400, 400);
    setVisible(true);
}

class CheckBoxRenderer implements TableCellRenderer
{
    JCheckBox   check   = new JCheckBox();

    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column)
    {
        if (row != 0 && obj instanceof Boolean)
        {
            check.setSelected((Boolean) obj);
            return check;
        }
        return null;
    }
}

public static void main(String[] args)
{
    Frame frame = new Frame();
    frame.initialize();
}

 }

So this gives me NullPointerException. And If I remove the setCellRenderer line it works fine. Can someone help me out

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:685)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:779)
at javax.swing.JComponent.paint(JComponent.java:1055)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5232)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5180)
at javax.swing.JComponent.paintImmediately(JComponent.java:4991)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

EDIT2: On googling I found this NullPointerException at javax.swing.plaf.synth.SynthContext.getPainter.

And If I do UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); which works fine. But I am using Ubuntu OS so I need GTK look. So I did UIManager.setLookAndFeel("ccom.sun.java.swing.plaf.gtk.GTKLookAndFeel"); but this again gives me the exception. I can't figure it out what is wrong here. I came to know about these packages from the actual implementation of getSystemLookAndFeelClassName()


Solution

  • This code works fine for me now

    class CheckBoxRenderer implements TableCellRenderer
    {
        JCheckBox   check   = new JCheckBox();
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column)
        {
            if (row != 0 && obj instanceof Boolean)
            {
                check.setSelected((Boolean) obj);
            }
    //      return null; this line gives NullPointerException
            return check;
        }
    }
    

    Finally, I figured out the problem. The problem is with the above line return null. Returning null is giving the NullPointerException. Because if we see the implementation of

    private void paintCell(SynthContext context, Graphics g, Rectangle cellRect, int row, int column) of SynthTableUI.class it has a line as

    Color b = component.getBackground(); but because I am returning null, it is trying to get the background of null. That is what the actual problem is. Thanks for all your help