Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

check for empty JOptionPane TextField

Posted on 2004-10-22
10
Medium Priority
?
547 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
Comment
Question by:komlaaa
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12387244
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
ID: 12387245
though u probably also want to catch NumberFormatException to handle when people enter non-numeric data
0
 

Author Comment

by:komlaaa
ID: 12387292
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12387323
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
ID: 12387334
> what is npe?

NullPointerException

0
 
LVL 92

Expert Comment

by:objects
ID: 12387336
> 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
ID: 12387379
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:
objects earned 1000 total points
ID: 12387445
i don't see anything there that would cause it to redisplay.
0
 
LVL 6

Expert Comment

by:mightyone
ID: 12387607
me neither
0
 

Author Comment

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

Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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