0

I'm trying to draw a rectangle on a JPanel. My code is:

public class Main {

    private JFrame frame;
    private Graphics g;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 788, 531);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.EAST);
        
        JLabel lblNewLabel = new JLabel("Black Wins");
        panel.add(lblNewLabel);
        
        JButton btnNewButton = new JButton("New button");
        panel.add(btnNewButton);
        
        JPanel panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(null);
        g = panel_1.getGraphics();
        g.setColor(Color.BLACK);
        g.drawRect(0, 20, 50, 40);
    }
}

And when I run it, I get this error message:

java.lang.NullPointerException: Cannot invoke "java.awt.Graphics.setColor(java.awt.Color)" because "this.g" is null at cf/cf.Main.initialize(Main.java:63) at cf/cf.Main.(Main.java:39) at cf/cf.Main$1.run(Main.java:26) at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

John Tippin
  • 41
  • 2
  • 5
  • The Swing components are not in a displayable state so there is no Graphics object for the panel. In any case you should NOT use getGraphcis() to do painting on the panel. Instead, you should be overriding the `paintComponent(...)` method of the panel. Read the section from the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more information and examples to get you started. – camickr Oct 20 '22 at 16:56

0 Answers0