check for empty JOptionPane TextField

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);
       }
komlaaaAsked:
Who is Participating?
 
objectsCommented:
i don't see anything there that would cause it to redisplay.
0
 
objectsCommented:
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
 
objectsCommented:
though u probably also want to catch NumberFormatException to handle when people enter non-numeric data
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
komlaaaAuthor Commented:
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
 
ThummalaRaghuveerCommented:
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
 
objectsCommented:
> what is npe?

NullPointerException

0
 
objectsCommented:
> 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
 
komlaaaAuthor Commented:
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
 
mightyoneCommented:
me neither
0
 
komlaaaAuthor Commented:
it is a very larger project so let me see where the bug may come from

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.