Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: How to display error message box

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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
    Last edited by jasonxman; August 19th, 2011 at 04:33 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to display error message box

    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

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    Last edited by Norm; August 19th, 2011 at 07:29 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display error message box

    Quote Originally Posted by Norm View Post
    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.

    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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to display error message box

    How do I tell what values that showInputDialog can return?

    showInputDialog = 1?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to display error message box

    Had nothing to do with null,
    Strange that you got a NullPointerException. You need a null to get that.

Similar Threads

  1. [SOLVED] Help making an error message.
    By Lost_Secret in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 1st, 2011, 04:48 PM
  2. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  3. an error message while runnung java
    By sravan_kumar343 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 25th, 2010, 10:19 AM
  4. help with a error message
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2010, 02:56 PM
  5. SOAP Message Factory response error
    By Buglish in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 6th, 2008, 01:42 PM