NullPointerException at java.awt.Window.access$700(Window.java:132) while painting JPanel
问题 I'm trying to paint component inside paint(Graphics) method of JPanel . The following code snippet works just fine, a JButton is painted nicely in my JPanel : @Override public void paint(Graphics g) { super.paint(g); JButton btn = new JButton("hello"); Dimension dim = btn.getPreferredSize(); btn.setSize(dim.width, dim.height); btn.paint(g); // paint the button } The code snippet works perfectly also for other components ( JLabel , JTree , ...) except JPanel . The following code will cause