bulenkov / Darcula

Darcula Look and Feel
Apache License 2.0
723 stars 114 forks source link

Directly painting a JProgressBar which has no parent component will throw NPE #51

Open FirokOtaku opened 5 years ago

FirokOtaku commented 5 years ago

If a JProgressBar has no parent component, trying to paint it on a Graphics will throw NullPointerException.

The following code

try
{
    UIManager.setLookAndFeel(DarculaLaf.class.getName());
}catch (Exception e){}

JProgressBar bar=new JProgressBar();
bar.setSize(1,1);
Graphics gra
    =new BufferedImage(bar.getWidth(),bar.getHeight(),BufferedImage.TYPE_INT_RGB)
    .getGraphics();
bar.print(gra);

will throw a NPE at this line: bar.print(gra);

Exception in thread "main" java.lang.NullPointerException
    at com.bulenkov.darcula.ui.DarculaProgressBarUI.paintDeterminate(DarculaProgressBarUI.java:124)
    at javax.swing.plaf.basic.BasicProgressBarUI.paint(BasicProgressBarUI.java:412)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at javax.swing.JComponent.printComponent(JComponent.java:1220)
    at javax.swing.JComponent.paint(JComponent.java:1060)
    at javax.swing.JComponent.print(JComponent.java:1202)
    at firok.test.Test.main(Test.java:27)

and here is the source code where program crashed ( DarculaProgressBarUI::paintDeterminate:124 )

g.setColor(c.getParent().getBackground());

for now the resolution is

AWTAccessor.getComponentAccessor().setParent(bar,jPanel);