Search code examples
javainstancemultiple-instancesinstanceof

Using the original instance of a JFrame


I have two Java classes; mcveF1 and mcveF2. The code below disables when run opens a JFrame with a singular JButton on it. This button opens a second JFrame and disables the first. Similarly this frame has a singular JButton on it. This button should close the second frame and re-enable the first frame. However an exception is thrown, java.lang.NullPointerException. I believe this is because I am creating a new instance of mcveF1 instead of using the current one. I am unaware of how to fix this and would appreciate any help in fixing it.

mcveF1

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

public class mcveF1
{
    public JFrame myMainWindow = new JFrame("Frame 1");

    JPanel panel2 = new JPanel();

    //Variables and Components
    JButton openFrame = new JButton("Open new frame");

    public void runGUI()
    {
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createSortTestPanel();

        myMainWindow.getContentPane().add(panel2);

        myMainWindow.setVisible(true);
        myMainWindow.pack();
        myMainWindow.setMinimumSize(new Dimension(myMainWindow.getBounds().getSize()));
        myMainWindow.setLocationRelativeTo(null);
    }

    public void createSortTestPanel()
    {
        MigLayout layout = new MigLayout("", "[grow]");
        panel2.setLayout(layout);

        openFrame.addActionListener(new buttonAction());
        panel2.add(openFrame);
    }

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

    class buttonAction implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            myMainWindow.setEnabled(false);
            mcveF2 f2 = new mcveF2();
            f2.runGUI();
        }
    }
}

mcveF2

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

public class mcveF2
{
    JFrame myMainWindow = new JFrame("Frame 2");

    JPanel panel2 = new JPanel();

    //Variables and Components
    JButton closeFrame = new JButton("Close");
    mcveF1 f1;

    public void runGUI()
    {
        myMainWindow.setLayout(new GridLayout(1,1));

        createSortTestPanel();

        myMainWindow.getContentPane().add(panel2);

        myMainWindow.setVisible(true);
        myMainWindow.pack();
        myMainWindow.setMinimumSize(new Dimension(myMainWindow.getBounds().getSize()));
        myMainWindow.setLocationRelativeTo(null);
    }

    public void createSortTestPanel()
    {
        MigLayout layout = new MigLayout("", "[grow]");
        panel2.setLayout(layout);

        closeFrame.addActionListener(new buttonAction());
        panel2.add(closeFrame);
    }

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

    class buttonAction implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            myMainWindow.dispose();
            f1.myMainWindow.setEnabled(true);
        }
    }
}

The null pointer exception is shown in this picture as per PM77-1's request.

null pointer exception


Solution

  • Create a constructor in your class mcveF2

    public class mcveF2() {
       public mcveF2(mcveF1 f1) {
          this.f1 = f1;
       }
    

    Then pass instance of mcveF1 to this constructor in button action listener.

    class buttonAction implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                myMainWindow.setEnabled(false);
                mcveF2 f2 = new mcveF2(mcveF1.this);
                f2.runGUI();
            }
        }