I am trying to remove a component from a jpanel but it keeps giving me a NullpointerException. I am trying to remove a component from the jpanel and then add a label in its place.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Minefield extends JFrame
{
private final int ROWS= 3;
private final int COLS=3;
public JButton[][] grid ;
private GridBagConstraints c;
private JPanel display;
public Minefield()
{
    grid = new JButton[ROWS][COLS];
    JPanel display = new JPanel();
    add(display);
    display.setLayout(new GridBagLayout());
     c = new GridBagConstraints();  
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.fill = GridBagConstraints.VERTICAL; 
    MouseHandler handler = new MouseHandler();
    for(int row=0;row<ROWS;row++)
    {
        for(int col =0;col<COLS;col++)
        {
            grid[row][col]=new JButton(""+row+","+col);
            c.gridx = col;  
            c.gridy = row;
            display.add(grid[row][col],c);
            grid[row][col].addMouseListener(handler);
        }
    }
}
private class MouseHandler  implements MouseListener
{
    public void mouseClicked(MouseEvent event)
    {
        for(int row=0;row<ROWS;row++)
        {
            for(int col =0;col<COLS;col++)
            {
                if(event.getSource()== grid[row][col])
                {
                    remove(grid[row][col]);
                    display.revalidate();
                }
            }
        }
    }
    public void mousePressed(MouseEvent me){}
    public void mouseReleased(MouseEvent me){}
    public void mouseEntered(MouseEvent me){}
    public void mouseExited(MouseEvent me){}
}
}
And here is the error it gives me
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Minefield$MouseHandler.mouseClicked(Minefield.java:48)
    at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)
    at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.desktop/java.awt.Component.processEvent(Unknown Source)
    at java.desktop/java.awt.Container.processEvent(Unknown Source)
    at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.desktop/java.awt.EventQueue.access$600(Unknown Source)
    at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
    at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
    at java.desktop/java.awt.EventQueue$5.run(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
Line 48 is display.revalidate().
 
    