即使在传入正确的变量

时间:2016-07-03 18:58:16

标签: java swing nullpointerexception

在下面的代码中,我试图设计一个全屏菜单。为此,我认为最好使用两个JPanels。我的问题是尝试在JPanels之间切换java.lang.NullPointerException。例外情况表明它出现在第101行,也就是这一行。 m.getMainWindow().getContentPane().setComponentZOrder(m.getOverlay(), 0);最初我认为问题是一个实例问题;但是,在传递menu变量(在main末尾的public class Menu void中使用)时,尽管所有相关方法仍然出现空指针。我很感激帮助解决我的问题。

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;

public class Menu {
    JFrame myMainWindow = new JFrame("Menu test");
    GridBagConstraints c;
    JLayeredPane lP;
    MyFirstPanel fP;
    MyOverlay oL;
    MigLayout baseLayout;
    MigLayout overlayLayout;

    private void runGUI(Menu m) {       
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myMainWindow.setLayout(new GridBagLayout());
        setContraints();
        createPanels(m);
        myMainWindow.getContentPane().add(fP, c);
        myMainWindow.getContentPane().add(oL, c);
        myMainWindow.pack();
        myMainWindow.setVisible(true);
    }

    private void setContraints() {
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;
    }

    private void createPanels(Menu m) {
        createFirstPanel(m);
        createOverlay();
    }

    private void createFirstPanel(Menu m) {
        baseLayout = new MigLayout("", "", "");
        fP = new MyFirstPanel(baseLayout, m);

    }

    private void createOverlay() {
        overlayLayout = new MigLayout("", "", "");
        oL = new MyOverlay(overlayLayout, true);

    }

    public JPanel getOverlay() {
        return oL;
    }

    public JFrame getMainWindow() {
        return myMainWindow;
    }

    public static void main(String[] args) {
        Menu menu = new Menu();
        menu.runGUI(menu);
    }
}

class MyFirstPanel extends JPanel {
    MyFirstPanel(MigLayout layout, Menu mM) {
        setLayout(layout);
        setBackground(Color.RED);
        setPreferredSize(new Dimension(600, 600));
        add(new JLabel("hiasdfadsf"), "wrap");
        JButton jb = new JButton("Press Me");
        jb.addActionListener(new CustomActionListener(mM));
        add(jb);
    }
}

class MyOverlay extends JPanel {
    MyOverlay(MigLayout layout, boolean visible) {
        setBackground(Color.GREEN);
        setPreferredSize(new Dimension(600, 600));
        add(new JLabel("hi"));

        setVisible(visible);
    }
}

class CustomActionListener implements ActionListener {
    Menu m;

    public CustomActionListener(Menu mM) {
        this.m = mM;
        m.getMainWindow().getContentPane().setComponentZOrder(m.getOverlay(), 0);
    }

    public void actionPerformed(ActionEvent e) {
        m.getMainWindow().getContentPane().setComponentZOrder(m.getOverlay(), 0);
    }
}

异常

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.setComponentZOrder(Container.java:760)
    at packages.CustomActionListener.<init>(Menu.java:101)
    at packages.MyFirstPanel.<init>(Menu.java:75)
    at packages.Menu.createFirstPanel(Menu.java:44)
    at packages.Menu.createPanels(Menu.java:38)
    at packages.Menu.runGUI(Menu.java:21)
    at packages.Menu.main(Menu.java:64)

1 个答案:

答案 0 :(得分:3)

方法getOverlay()返回null。它返回的叠加层是在

中创建的
private void createPanels(Menu m) {
    createFirstPanel(m);
    createOverlay();
}

因此,在第一个面板之后,会创建叠加层。但是第一个面板的创建需要叠加层存在。反转这两个指令应解决问题。