I've been making a Minesweeper clone in Java and everything has been going relatively well until I added support for saving and loading files. Saving and Loading the files works as intended, but the issue occurs when I load up the game, perform the initial click, and then exit. I'm presented with this error but it does not always occur:
Exception while removing reference.
If I never declare JFileChooser in the constructor, I never get the error. When I do get the error, it never prints the stack trace either or any information regarding it.
public class FileMenu extends JMenuBar implements ActionListener
{
private static final long serialVersionUID = 459585961076461945L;
private JFileChooser fileChooser;
private Path dir = Paths.get(".");
private File file;
private MineFrame frame;
private JMenuItem easy, medium, hard,loadItem, exitItem, saveItem;
private FileNameExtensionFilter filter;
private JMenu menu, newGameMenu;
private ObjectInputStream in;
private ObjectOutputStream out;
private FileDialog saveDialog;
public FileMenu(MineFrame frame)
{
super();
this.frame = frame;
exitItem = new JMenuItem("Quit Game");
saveItem = new JMenuItem("Save Game");
loadItem = new JMenuItem("Load Game");
easy = new JMenuItem("Beginner: 10 x 10. 10 Mines");
medium = new JMenuItem("Intermdiate: 16 x 16. 40 Mines");
hard = new JMenuItem("Expert: 16 x 30. 99 Mines");
fileChooser = new JFileChooser(dir.toString());
menu = new JMenu("File");
newGameMenu = new JMenu("New Game");
add(menu);
menu.add(newGameMenu);
menu.add(saveItem);
menu.add(loadItem);
menu.add(exitItem);
newGameMenu.add(easy);
newGameMenu.add(medium);
newGameMenu.add(hard);
easy.addActionListener(this);
medium.addActionListener(this);
hard.addActionListener(this);
saveItem.addActionListener(this);
loadItem.addActionListener(this);
exitItem.addActionListener(this);
}
This is the only location where the call to FileMenu() occurs
public class MineFrame extends JFrame
{
private static final long serialVersionUID = 4816731330494815932L;
private MineBoard board;
private JLabel mineLabel;
private MinePanel minePanel;
private JPanel fileBarPanel, mainPanel, labelPanel;
private FileMenu menu;
private BorderLayout layout;
public MineFrame(MineBoard board)
{
super("Minesweeper Java");
this.board = board;
fileBarPanel = new JPanel();
mainPanel = new JPanel();
labelPanel = new JPanel();
menu = new FileMenu(this);
layout = new BorderLayout();
mineLabel = new JLabel();
minePanel = new MinePanel(this);
labelPanel.add(mineLabel);
labelPanel.setBackground(Color.WHITE);
mineLabel.setForeground(Color.RED);
mineLabel.setFont(new Font("Times", Font.BOLD, 20));
fileBarPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
fileBarPanel.add(menu);
minePanel.setBackground(Color.WHITE);
mainPanel.setLayout(new GridLayout(2, 1));
mainPanel.add(fileBarPanel);
mainPanel.add(labelPanel);
setLayout(layout);
add(mainPanel, BorderLayout.NORTH);
add(minePanel, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.WHITE);
setVisible(true);
setResizable(false);
setSize(500,500);
}
Interestingly enough, the error does not occur with FileDialog. I've searched google for it extensively and it's seemingly rare.
UPDATE Moving the object into the actionPerformed method did the trick somehow!