Reputation: 13
I am trying to create an if else statement that checks multiple values, one after another, to see whether they are null or not.
The if else statement that I created worked perfectly earlier, but after restarting my pc it no longer works.
Any help is appreciated.
Here is my code:
//Declare Strings
String ProjectNo, Location, VP;
//Store values from text boxes into strings
ProjectNo = txtProjectNum.getText();
Location = txtLocation.getText();
VP = txtVPNo.getText();
if(ProjectNo == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else if (Location == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else if (VP == null){
JOptionPane.showMessageDialog(null, "No fields can be left empty!");
}
else{
JOptionPane.showMessageDialog(null, "All fields have been filled in!");
}
Upvotes: 1
Views: 59
Reputation: 1074276
I'm fairly sure the getText
method never returns null
. If you want to check for an empty field, check .length() == 0
or .isEmpty
. You might consider .trim()
first. E.g.:
if (projectNo.trim().isEmpty()) {
Or perhaps trim
when getting the text into the variable:
projectNo = txtProjectNum.getText().trim();
// ...
if (projectNo.isEmpty()) {
Upvotes: 3