Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

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);
       }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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);
       }
though u probably also want to catch NumberFormatException to handle when people enter non-numeric data
Avatar of komlaaa
komlaaa

ASKER

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
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);
       }


> what is npe?

NullPointerException

> 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.
Avatar of komlaaa

ASKER

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);
                       .
                       .
                       .
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
me neither
Avatar of komlaaa

ASKER

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

Thanks