Reputation:
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";
}
Upvotes: 0
Views: 607
Reputation: 343
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
Upvotes: 1