Solved

check for empty JOptionPane TextField

Posted on 2004-10-22
535 Views
Last Modified: 2008-03-03
Hi Experts!!
Here is a simple code i am using to text for non empty JOptionPane textField, for some reason, my code keep crashing, please tell me what is wrong.

=====================CODE======================
String numText = JOptionPane.showInputDialog(null, "Number of Side?");  
       int num = Integer.parseInt(numText);
      if( (numText == null) || (numText.trim().equals("")) || (num < 4)  ){
           JOptionPane.showMessageDialog (null, "Please An Integer > 4");
           System.exit(0);
       }
0
Question by:komlaaa
    10 Comments
     
    LVL 92

    Expert Comment

    by:objects
    if it returns null you'll get a npe on the second line, try something like

    String numText = JOptionPane.showInputDialog(null, "Number of Side?");  
          if( (numText == null) || (numText.trim().equals("")) || (Integer.parseInt(num) < 4)  ){
               JOptionPane.showMessageDialog (null, "Please An Integer > 4");
               System.exit(0);
           }
    0
     
    LVL 92

    Expert Comment

    by:objects
    though u probably also want to catch NumberFormatException to handle when people enter non-numeric data
    0
     

    Author Comment

    by:komlaaa
    Thanks Object,
    what is npe?
    Also,  Even if i enter a correct value, the input box comeback for the second time,
    Do u know why?  ========= Notice i increase the point  :)

    Thanks
    komlaaa
    0
     
    LVL 2

    Expert Comment

    by:ThummalaRaghuveer
    As said by objects U should catch NumberFormatException or else program crashes if its value is null and U will never reach the if condition even.

    I guess U are looking for user to enter a number in this box......

    String numText = JOptionPane.showInputDialog(null, "Number of Side?");  
    try{
           int num = Integer.parseInt(numText);
    }
    catch(NumberFormatException e1234){
    System.out.println("Null");
    numText == null;
    }
          if( (numText == null) || (numText.trim().equals("")) || (num < 4)  ){
               JOptionPane.showMessageDialog (null, "Please An Integer > 4");
               System.exit(0);
           }


    0
     
    LVL 92

    Expert Comment

    by:objects
    > what is npe?

    NullPointerException

    0
     
    LVL 92

    Expert Comment

    by:objects
    > Even if i enter a correct value, the input box comeback for the second time,

    need to see some more of your code to tell whats happening.
    0
     

    Author Comment

    by:komlaaa
    Here is some code:

     public DicePanel() {

             setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
          setBorder(BorderFactory.createEmptyBorder(30,30,30,30));

            JButton rollButton = new JButton("Roll");
            rollButton.setActionCommand("rollDie");
            rollButton.setFont(new Font("Sansserif", Font.PLAIN, 26));
            rollButton.addActionListener(this);
            add(rollButton, BorderLayout.NORTH);
       
            int num = 0;

           String numText = JOptionPane.showInputDialog(null, "Number of Side?");
            //int num = Integer.parseInt(numText);
         if( (numText == null) || (numText.trim().equals(""))
         || ((num = Integer.parseInt(numText)) < 4)  ){
               JOptionPane.showMessageDialog (null, "Please An Integer > 4");
               System.exit(0);
           }

                firstDie = new Die(num);
                secondDie = new Die(num);
                           .
                           .
                           .
    0
     
    LVL 92

    Accepted Solution

    by:
    i don't see anything there that would cause it to redisplay.
    0
     
    LVL 6

    Expert Comment

    by:mightyone
    me neither
    0
     

    Author Comment

    by:komlaaa
    it is a very larger project so let me see where the bug may come from

    Thanks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Conflicting setter definitions for property Exception 5 26
    only14 challenge 19 50
    pre4 challenge 19 53
    withoutTen challenge 14 50
    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This video teaches viewers about errors in exception handling.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now