0

EDIT: Not sure if codependent is the right word here, but this i mean something like menu1(menu2) & menu2(menu1)

I'm creating a simple program where I have:

A separate class (Game) that extends J Frame and initializes 2 other classes (Menu1, Menu2) 2 separate classes (Menu 1, Menu 2) that extend J Frame, each one with 1 jbutton 2 action listeners that can be initialized anywhere (so no limitations), that are listening on a button on one menu, but need to change something in the second menu and vice versa The specificities are:

The menus MUST be initialized in the Game class The three classes must be separate java classes the action listeners must change something in the other menu My code is:

Game class that the menus need to be initialized in:

public class Game extends JFrame {
    Menu1 menu1;
    Menu2 menu2;

    public Game(){
        setSize(300,400);
        setDefaultCloseOperation(3);
         menu1 = new Menu1(menu2);
         menu2 = new Menu2(menu1);

        add(menu2,BorderLayout.SOUTH);
        add(menu1,BorderLayout.NORTH);


    }


    public static void main(String[] args) {
        Game game = new Game();
        game.setVisible(true);
    }


}
The first Menu with Jbutton and action listener

public class Menu1 extends JPanel {
    Menu2 menu2;


    public Menu1(Menu2 menu2) {
        this.menu2 = menu2;
        JButton button1 = new JButton("Hello1");
        Al1 al1 = new Al1();
        button1.addActionListener(al1);
        add(button1);
        setVisible(true);
    }
    class Al1 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            menu2.setBackground(Color.red);
        }
    }
} 
The second menu that is pretty much analog to the first one

public class Menu2 extends JPanel {
    Menu1 menu1;


    public Menu2(Menu1 menu1) {
        this.menu1 = menu1;
        JButton button2 = new JButton("Hello2");
        Al2 al2 = new Al2();
        button2.addActionListener(al2);
        add(button2);
        setVisible(true);
    }
    class Al2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            menu1.setBackground(Color.red);
        }
    }
}

When i click on the JButton initialized in menu2 the background on the first one changes, just as intended, but when i click on the first JButton, on the top, i get this error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "Menu2.setBackground(java.awt.Color)" because "this.this$0.menu2" is null at Menu1$Al1.actionPerformed(Menu1.java:21) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)

Im assuming this is happening because im initializing both classes with each other as a parameter, it would work alright for menu2 because menu 1 has already been initialized, but for menu 1, it's a bit tricky because menu 2 doesn't yet "exist"

My question is: How can i successfully initialize these codependent classes with each other as a parameter, and successfully run the program.

OR/AND

what workaround can I use to not have to have 2 codependent classes. But still have the A) the action listener be connected to both classes and B) have the 2 menu classes be separate java classes.

Teo
  • 45
  • 4
  • Simply move the swing code out of constructors into some `init` method and call it separately. – JockX Jan 13 '22 at 23:07
  • When you call this: `menu1 = new Menu1(menu2);`, the `menu2` variable holds a null value, and this is causing your NPE. See the duplicates to see various ways of creating (or avoiding) circular references. – Hovercraft Full Of Eels Jan 13 '22 at 23:08

0 Answers0