Struts——从 s:submit 获取 NullPointerException

发布于 2024-10-19 18:22:34 字数 1059 浏览 1 评论 0原文

还有一个问题与我的 Struts 表单相关,我将继续讨论其他问题。我终于弄清楚如何实现带有两个按钮的表单。我将用户单击的 s:submit 按钮的 name 属性发送到我的操作类的execute() 方法。我有一个与每个按钮关联的字符串变量,并且所选按钮的变量(很明显)是被设置的变量,并且我希望为每个按钮执行不同的方法。当我点击第一个按钮时,没有问题。当我点击第二个按钮时出现问题。我收到与第一个字符串关联的 NullPointerException。我可以发誓有问题的字符串一开始是空的,这就是我要检查的内容,所以我不明白为什么会出现问题。我包括 getter 和 setter 以及execute() 方法。有什么想法吗?

public String getApprove() {
    return approve;
}

public void setApprove(String approve) {
    this.approve = approve;
}

public String getDeny() {
    return deny;
}

public void setDeny(String deny) {
    this.deny = deny;
}

public String execute() {
    BulletinDAO bulletinDAOInstance = new BulletinDAO();

    <!-- Error occurs here when approve is null -->
    if (! approve.equals(null)) {
        if (bulletinDAOInstance.approveBulletin(id) == true) {
            return "success";
        }           
    }

    if (! deny.equals(null)) {
        if (bulletinDAOInstance.denyBulletin(id) == true) {
            return "success";
        }                       
    }

    return "failure";
}

One more question related to my Struts form, and I will move on to other things. I finally figured out how to implement a form with two buttons. I am sending the name attribute of the s:submit button clicked on by the user to the execute() method of my action class. I have a String variable associated with each button, and the variable of the selected button is (quite obviously) the one that gets set, and I want a different method performed for each button. When I click on the first button, no problem. The problem occurs when I click on the second button. I get a NullPointerException associated with the first String. I could swear the String in question started out as null, and that's what I'm checking for, so I can't see why there would be a problem. I am including the getters and setters as well as the execute() method. Any ideas out there?

public String getApprove() {
    return approve;
}

public void setApprove(String approve) {
    this.approve = approve;
}

public String getDeny() {
    return deny;
}

public void setDeny(String deny) {
    this.deny = deny;
}

public String execute() {
    BulletinDAO bulletinDAOInstance = new BulletinDAO();

    <!-- Error occurs here when approve is null -->
    if (! approve.equals(null)) {
        if (bulletinDAOInstance.approveBulletin(id) == true) {
            return "success";
        }           
    }

    if (! deny.equals(null)) {
        if (bulletinDAOInstance.denyBulletin(id) == true) {
            return "success";
        }                       
    }

    return "failure";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2024-10-26 18:22:34

approve.equals(null) 永远不会为 true,如果approve 为 null 将导致空指针异常,因为您试图在 null 对象上调用函数。

使用 if(approve == null) 比较内存中的位置而不是对象的内容

approve.equals(null) will never be true, if approve is null that will cause null pointer exception because you're trying to call a function on a null object.

use if(approve == null) which compares the location in memory instead of the contents of the object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文