Java if语句总是返回true

Java if语句总是返回true,java,if-statement,null,Java,If Statement,Null,我试图通过检查空值来验证Swing表单中的一些输入。但是,checkFirstName方法总是返回true。例如,如果我将表单上的firstname留空,即使字段为空,它也会返回true 这是我的两个方法,第一个是在用户单击save按钮时触发的 public void saveNewCustomer() throws SQLException, ClassNotFoundException { boolean dbOK = false;//boolean to check if

我试图通过检查空值来验证Swing表单中的一些输入。但是,checkFirstName方法总是返回true。例如,如果我将表单上的firstname留空,即使字段为空,它也会返回true

这是我的两个方法,第一个是在用户单击save按钮时触发的

public void saveNewCustomer() throws SQLException, ClassNotFoundException {

        boolean dbOK = false;//boolean to check if data input is not null

        System.out.println(dbOK);
        String firstName = txtNCustomerFirstName.getText();
        String lastName = txtNCustomerLastName.getText();

        if (checkFirstName(firstName)) {
            dbOK = true;
        } else {
            lblNCustFirstNameError.setText("First Name Must be Entered");
            dbOK = false;
        }
        System.out.println(dbOK);

        if (dbOK) {
            dbConnector.insertSignup(firstName, lastName);
            System.out.println("Success");
        } else {
            System.out.println("Error");
        }
    }

    public boolean checkFirstName(String firstName) {
        boolean allOK = false;
        System.out.println(allOK);

        if (firstName != null) {
            allOK = true;
        } else {
            allOK = false;
        }
        return allOK;
    }

我是否做了一些错误的事情,因为这对我来说应该是返回false,因为firstname字段为null。

字符串永远不会为null,
字符串
将为空。选中
firstName.isEmpty()
。不过,我建议您也保留空值检查:

public boolean checkFirstName(String firstName) {
    boolean allOK = false;
    System.out.println(allOK);

    if (firstName != null && !firstName.isEmpty()) {
        allOK = true;
    } else {
        allOK = false;
    }
    return allOK;
}
编辑:正如Windle所指出的,您可能希望检查字符串是否至少有一个非空白:

if (firstName != null && !firstName.trim().isEmpty())

此外,您还可以执行更复杂的验证-例如,如果您希望在修剪用户名后确保用户名中没有空格。

字符串永远不会为空,字符串将为空。选中
firstName.isEmpty()
。不过,我建议您也保留空值检查:

public boolean checkFirstName(String firstName) {
    boolean allOK = false;
    System.out.println(allOK);

    if (firstName != null && !firstName.isEmpty()) {
        allOK = true;
    } else {
        allOK = false;
    }
    return allOK;
}
编辑:正如Windle所指出的,您可能希望检查字符串是否至少有一个非空白:

if (firstName != null && !firstName.trim().isEmpty())
此外,您还可以执行更复杂的验证—例如,如果您希望确保在修剪用户名后,用户名中没有空格

例如,如果我在表格上把名字留空

您只是在检查
null
,还需要执行空
(“”)
字符串检查

应该是这样的:

if (firstName != null && !"".equals(firstName.trim()) {
例如,如果我在表格上把名字留空

您只是在检查
null
,还需要执行空
(“”)
字符串检查

应该是这样的:

if (firstName != null && !"".equals(firstName.trim()) {

txtNCustomerFirstName.getText
方法返回一个空字符串。您可以更改
checkFirstName
方法的if语句来检查空字符串:

if (firstName != null && !firstName.isEmpty()) {

如果没有Java 6+,可以使用
firstName.length()>0
而不是
isEmpty

txtNCustomerFirstName.getText
方法返回空字符串。您可以更改
checkFirstName
方法的if语句来检查空字符串:

if (firstName != null && !firstName.isEmpty()) {

如果您没有Java 6+,您可以使用
firstName.length()>0
而不是
isEmpty

据我所知,您在saveNewCustomer()中调用的getText()方法将返回空字符串,而不是null。

据我所知,您在saveNewCustomer()中调用的getText()方法将返回空字符串而不是null

例如,如果我在表格上把名字留空,它会 即使字段为空,也返回true

这就是你的推理错误的地方。将文本字段留空时,
getText()
返回一个空字符串,即
”,该字符串不为空。您应该检查是否为空,而不是
null

if (!firstName.isEmpty()) { ... }
如果
firstName
也可能是
null
(例如,当它来自另一个源时),您应该事先检查:

if (firstName != null && !firstName.isEmpty()) { ... }
例如,如果我在表格上把名字留空,它会 即使字段为空,也返回true

这就是你的推理错误的地方。将文本字段留空时,
getText()
返回一个空字符串,即
”,该字符串不为空。您应该检查是否为空,而不是
null

if (!firstName.isEmpty()) { ... }
如果
firstName
也可能是
null
(例如,当它来自另一个源时),您应该事先检查:

if (firstName != null && !firstName.isEmpty()) { ... }

正如其他答案所建议的那样,使用空字符串检查执行空检查。我会说在执行空检查之前也要修剪前导空格和尾随空格,因为实际上您希望避免这种情况

if (firstName != null && !firstName.trim().isEmpty()) { ... }

正如其他答案所建议的那样,使用空字符串检查执行空检查。我会说在执行空检查之前也要修剪前导空格和尾随空格,因为实际上您希望避免这种情况

if (firstName != null && !firstName.trim().isEmpty()) { ... }

firstname
真的是
null
还是空的?我建议使用ApacheCommons中的
StringUtils
类:您是否尝试过
!firstName.isEmpty()
?调试器应该是您最好的朋友…您可以缩短代码。相反,
boolean标志=false;if(contidion){flag=true;}else{flag=false;}返回标志使用
返回条件
firstname
真的
null
还是只是空的?我建议使用ApacheCommons中的
StringUtils
类:您是否尝试过
!firstName.isEmpty()
?调试器应该是您最好的朋友…您可以缩短代码。相反,
boolean标志=false;if(contidion){flag=true;}else{flag=false;}返回标志使用
返回条件@Nambari谢谢你。太多的语言。。。我不时感到困惑:)修正了。@Nambari@predi也谢谢你。我知道我不会那么困惑:)+1是
!firstName.isEmpty()
我们到底想要什么?因为OP正在检查一个名称,所以不会
!firstName.trim().isEmpty()
更接近正确的东西吗?当然,您可以进行更复杂的验证,但至少也可以捕获字符串的空白。更好的是,将
txtNCustomerFirstName.getText()
更改为
txtNCustomerFirstName.getText().trim()
从一开始就解决了这个问题。@Nambari谢谢你。太多的语言。。。我不时感到困惑:)修正了。@Nambari@predi也谢谢你。我知道我不会那么困惑:)+1是
!firstName.isEmpty()
我们到底想要什么?因为OP正在检查一个名称,所以不会
!firstName.trim().isEmpty()
更接近正确的东西吗?当然,您可以进行更复杂的验证,但至少也可以捕获字符串的空白。