从jpanel删除组件时发生NullPointerException

时间:2018-08-17 13:38:05

标签: java swing

我正在尝试从jpanel中删除一个组件,但是它一直给我一个def equations(p, t, lmbds): return np.array([-p[0] * lmbds[0] + p[1] * lmbds[1], -p[1] * lmbds[1] - p[1] * lmbds[2] + p[0] * lmbds[0]]) 。我试图从jpanel中删除一个组件,然后在其位置添加标签。

NullpointerException

这是它给我的错误

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){}
}

}

第48行是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)

1 个答案:

答案 0 :(得分:4)

您已经声明了两个名为display的变量:

public class Minefield extends JFrame
{
    // ...

    private JPanel display;
    public Minefield()
    {
        grid = new JButton[ROWS][COLS];
        JPanel display = new JPanel();
        // ...

第二个变量在构造函数中是局部的。调用display.revalidate()时,您正在访问的private字段尚未初始化。

要解决此问题,您可以删除声明:

display = new JPanel();

更好的是,您可以内联初始化字段:

public class Minefield extends JFrame
{
    // ...
    private JPanel display = new JPanel();

然后从构造函数中删除行JPanel display = new JPanel();

注意

您在mouseClicked()中所做的工作比必要的多。您不需要任何for循环。您可以直接使用event.getSource()来达到相同的效果:

private class MouseHandler  implements MouseListener
{
    public void mouseClicked(MouseEvent event)
    {
        remove(event.getSource());
        display.revalidate();
    }
    public void mousePressed(MouseEvent me){}
    public void mouseReleased(MouseEvent me){}
    public void mouseEntered(MouseEvent me){}
    public void mouseExited(MouseEvent me){}
}

仔细检查后,我发现此代码和您的原始代码都存在一个缺陷,可能会影响应用程序其他未在此处显示(或尚未编写)的部分。 mouseClicked()函数将删除显示的按钮,但是您仍然可以参考grid中的按钮。如果您尝试访问grid数组中的按钮并假定它们在JPanel中呈现,则可能会导致问题。