Hey everyone,
I am having trouble exiting a program normally, rather than it crashing whenever I click the 'cancel' button.
This code here is an example of what i want to do/happen...
The code above works and doesn't crash or have syntax errorsdouble length = 0; // passing into String user_input; // passing from boolean valid = true; // to exit the loop (irrelevant) do { user_input = JOptionPane.showInputDialog("Enter the length in metres: "); try { length = Double.parseDouble(user_input); // code to pass string into double. valid = true; } catch(NullPointerException error) // catches the error when user clicks the 'cancel' button and exits the program normally. { System.exit(0); }
I am trying to implement this with the code below, but to no avail.
public static void exampleMethod(String user_input) { String choice; try { choice = String.parseString(user_input); //<--- No such thing?! WHY!? } catch(NullPointerException error) // catches the error when user clicks the 'cancel' button and exits the program normally. { System.exit(0); }
It honestly doesn't bother me what the 'try' function does, it can do anything... or NOTHING i just want the program NOT to crash when i click the 'cancel' button. I even attempted to delete it but the compiler insists ('catch' with no 'try')
I hope this is clear enough...