dispose()'ing a frame from itself.
EDIT: im trying to close one JFrame while having my window controller draw another to replace it. i've revised my code from the original implementation, and it "works", but throws a null pointer exception. here's the revised bit:
Code:
//from the frame to be closed
//get the difficulty from the user
difficulty = (String) JOptionPane.showInputDialog(null, "What difficulty would you like?", "Game",
JOptionPane.QUESTION_MESSAGE, null, dificultyChoices, dificultyChoices[0]);
//if the user presses the cancel button, present them with the main menu
if(difficulty == null)
wc.canceled(this);
Code:
//from the window controller
public void canceled(JFrame callingWindow)
{
callingWindow.dispose();
//createWindow() draws the window i want to replace callingWindow
createWindow();
}
when i run that and click that particular cancel button, eclipse gives me
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
with the first location being the very next line of code after wc.canceled(this);. it also cites two other places in my code and a couple dozen in the swing and awt libraries.
the odd thing, though, is that it works until i go to actually start the game.
whats actually happening?
-----------------original post-----------------
so, im trying to catch a cancel button click from a JOptionPane, then send the exception up to the window controller, have it destroy the JFrame the exception came from, then start over with the main JFrame of the program.
as that's probably convoluted, i'll post some code.
Code:
//from inside the JFrame
try
{
difficulty = (String) JOptionPane.showInputDialog(null, "What difficulty would you like?", "Game",
JOptionPane.QUESTION_MESSAGE, null, dificultyChoices, dificultyChoices[0]);
}
catch(HeadlessException ex)
{
//wc is a window controller
wc.canceled(this);
}
Code:
//from the window controller
public void canceled(JFrame callingWindow)
{
callingWindow.dispose();
//createWindow() draws the window i want to replace callingWindow
createWindow();
}
the problem with this is that the original JFrame goes right ahead executing its next line of code. but, of course, since it's just been dispose()'d, it throws a NullPointerException and crashes.
how should i actually be implementing this?