-2

In the following code I am trying to design a full screen menu. To do this I thought it would be best to use two JPanels. My problem is when trying to switch between the JPanels I get a java.lang.NullPointerException. The exception states that it occurs on line 101 which is this line. m.getMainWindow().getContentPane().setComponentZOrder(m.getOverlay(), 0); Originally I believed to problem to be an instance problem; however, upon passing the menu variable, which is used in the main void at the end of the public class Menu, though all the relevant methods the null pointer still occurs. I would appreciate any help in fixing my problem.

Code

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

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)
Dan
  • 7,286
  • 6
  • 49
  • 114
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Frakcool Jul 03 '16 at 19:03
  • 1
    Don't try to play with the ZOrder. If you want to swap panels then use a [Card Layout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Also, when you post the code should only contain JDK classes. MigLayout is not part of the JDK. People can't test your code. – camickr Jul 03 '16 at 19:17
  • @camickr Thank you for the info and I will leave mig layout out next time – Dan Jul 03 '16 at 19:18

1 Answers1

3

The method getOverlay() returns null. The overlay it returns is created in

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

So the overlay is created after the first panel. But the creation of the first panel needs the overlay to exist. Inverting those two instructions should fix the problem.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • In doing this a `java.lang.IllegalArgumentException: component and container should be in the same top-level window` is created – Dan Jul 03 '16 at 19:07
  • That's another matter. Try understanding what it means by examining the stack trace, reading the javadoc, etc. If you're stuck, ask another question. – JB Nizet Jul 03 '16 at 19:12
  • No Problem. Will do that – Dan Jul 03 '16 at 19:13