I’m trying to receive input from a java dialog and I can’t seem to catch the NullPointerException when the box is closed, can anyone help?
private static final String DEFAULTNAME = "Player001";
public class Player implements Serializable
{
private String name;
private int score;
public Player(String Pname,int Pscore)
{
name = Pname;
score = Pscore;
}
}
try
{
person = new Player(JOptionPane.showInputDialog("Please enter your name"),0);
}
catch(NullPointerException e)
{
person = new Player(DEFAULTNAME,0);
}
catch(Throwable t)
{
person = new Player(DEFAULTNAME,0);
}
Does anyone have a solution or is there a way of making the dialog unable to close?
you are better off checking the name for invalid values before adding it in