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.showInputDialo g(null, "Number of Side?");
int num = Integer.parseInt(numText);
if( (numText == null) || (numText.trim().equals("") ) || (num < 4) ){
JOptionPane.showMessageDia log (null, "Please An Integer > 4");
System.exit(0);
}
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.showInputDialo
int num = Integer.parseInt(numText);
if( (numText == null) || (numText.trim().equals("")
JOptionPane.showMessageDia
System.exit(0);
}
though u probably also want to catch NumberFormatException to handle when people enter non-numeric data
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
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.showInputDialo g(null, "Number of Side?");
try{
int num = Integer.parseInt(numText);
}
catch(NumberFormatExceptio n e1234){
System.out.println("Null") ;
numText == null;
}
if( (numText == null) || (numText.trim().equals("") ) || (num < 4) ){
JOptionPane.showMessageDia log (null, "Please An Integer > 4");
System.exit(0);
}
I guess U are looking for user to enter a number in this box......
String numText = JOptionPane.showInputDialo
try{
int num = Integer.parseInt(numText);
}
catch(NumberFormatExceptio
System.out.println("Null")
numText == null;
}
if( (numText == null) || (numText.trim().equals("")
JOptionPane.showMessageDia
System.exit(0);
}
> what is npe?
NullPointerException
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.
need to see some more of your code to tell whats happening.
ASKER
Here is some code:
public DicePanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(BorderFactory.cr eateEmptyB order(30,3 0,30,30));
JButton rollButton = new JButton("Roll");
rollButton.setActionComman d("rollDie ");
rollButton.setFont(new Font("Sansserif", Font.PLAIN, 26));
rollButton.addActionListen er(this);
add(rollButton, BorderLayout.NORTH);
int num = 0;
String numText = JOptionPane.showInputDialo g(null, "Number of Side?");
//int num = Integer.parseInt(numText);
if( (numText == null) || (numText.trim().equals("") )
|| ((num = Integer.parseInt(numText)) < 4) ){
JOptionPane.showMessageDia log (null, "Please An Integer > 4");
System.exit(0);
}
firstDie = new Die(num);
secondDie = new Die(num);
.
.
.
public DicePanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBorder(BorderFactory.cr
JButton rollButton = new JButton("Roll");
rollButton.setActionComman
rollButton.setFont(new Font("Sansserif", Font.PLAIN, 26));
rollButton.addActionListen
add(rollButton, BorderLayout.NORTH);
int num = 0;
String numText = JOptionPane.showInputDialo
//int num = Integer.parseInt(numText);
if( (numText == null) || (numText.trim().equals("")
|| ((num = Integer.parseInt(numText))
JOptionPane.showMessageDia
System.exit(0);
}
firstDie = new Die(num);
secondDie = new Die(num);
.
.
.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
me neither
ASKER
it is a very larger project so let me see where the bug may come from
Thanks
Thanks
String numText = JOptionPane.showInputDialo
if( (numText == null) || (numText.trim().equals("")
JOptionPane.showMessageDia
System.exit(0);
}