How to display error message box
What I'm trying to figure out is "How do I make it where if the user hits Cancel on either first or last name, show an error message. ( The one with an X )
Here is my code:
import javax.swing.JOptionPane;
public class Assign2
{
// main method begins execution of Java application
public static void main(String[] args)
{
/* assigned a string called "name" and this will prompt the user to
* input there first name in the dialog box.
*/
String name = JOptionPane.showInputDialog("Please enter your first name:");
/* assigned a string called "name2" and this will prompt the user to
* input there last name in the dialog box.
*/
String name2 = JOptionPane.showInputDialog("Please enter your last name:");
// lastly, this will prompt the user with a welcome message
JOptionPane.showMessageDialog(null, "Hello, " + name + " " + name2 + ",\nWelcome.");
}// end method main
}// end class Assign2
Re: How to display error message box
Quote:
if the user hits Cancel
How do you detect if the user hit the Cancel button? Does the method you have called return any indication of that? If so, you can use that information to make your decision.
Pseudo code:
show message
get results
if results is a cancel, then show error message
Re: How to display error message box
Sorry, still can't figure it out. If you were to explain to me showing code I would appreciate it.
Re: How to display error message box
Which part don't you understand?
Detecting when the user pressed Cancel?
Have you read ALL the API doc for the method you are using? It is explained there.
Displaying an error message?
Again read the API doc. It is very similar to what you do to ask a question.
Re: How to display error message box
I tried inputting this code in after string name2
if(name.isEmpty() || name2.isEmpty()){
// Execute error code
JOptionPane.showMessageDialog(null, "Error: You need to fill in both inputs to continue! The program will now exit.");
System.exit(0);
I get this error in the console..
Exception in thread "main" java.lang.NullPointerException
at Assign2.main(Assign2.java:25)
Re: How to display error message box
What variable has a null value at line 25? Check back in your code to see why that variable does not have a valid value and change the code so that it does have a valid value.
What are the values that the showInputDialog can return? Can it return a null value?
Re: How to display error message box
Quote:
Originally Posted by
Norm
What variable has a null value at line 25? Check back in your code to see why that variable does not have a valid value and change the code so that it does have a valid value.
What are the values that the showInputDialog can return? Can it return a null value?
I have no idea what I'm doing wrong can you show me the code that is correct.
Code :
import javax.swing.JOptionPane;
public class Assign2
{
// main method begins execution of Java application
public static void main(String[] args)
{
/* assigned a string called "name" and this will prompt the user to
* input there first name in the dialog box.
*/
String name = JOptionPane.showInputDialog("Please enter your first name:");
/* assigned a string called "name2" and this will prompt the user to
* input there last name in the dialog box.
*/
String name2 = JOptionPane.showInputDialog("Please enter your last name:");
if(name.isEmpty() || name2.isEmpty()){
// Execute error code
JOptionPane.showMessageDialog(null, "Error: You need to fill in both inputs to continue! The program will now exit.");
System.exit(0);
}
// lastly, this will prompt the user with a welcome message
JOptionPane.showMessageDialog(null, "Hello, " + name + " " + name2 + ",\nWelcome!");
}// end method main
}// end class Assign2
Re: How to display error message box
Your code does not consider this:
What are the values that the showInputDialog can return? Can it return a null value?
A variable with a null value can not be used to call a method: name.isEmpty()
if name is null you will get a NullPointerException
Test the value of name before using it to call a method
Re: How to display error message box
How do I tell what values that showInputDialog can return?
showInputDialog = 1?
Re: How to display error message box
Read the API doc for the class. Go to this site and Find the JOptionPane class.
Java Platform SE 6
and then find the method in that classs that you are interested in.
Re: How to display error message box
Thanks I figured it out. Had nothing to do with null, it was a matter of fixing the if statement.
Re: How to display error message box
Quote:
Had nothing to do with null,
Strange that you got a NullPointerException. You need a null to get that.