• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

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);
       }
0
komlaaa
Asked:
komlaaa
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
objectsCommented:
i don't see anything there that would cause it to redisplay.
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now