Getting a null pointer exception. I've spent hours trying to figure out the issue, but I just don't have the experience to properly trouble shoot it. The stack trace is below, and below that is the code for my DirectAction. The console says the error occurs on line 55, which is "if (errorMessage != null || errorMessage.length() != 0) { "
The thing is, errorMessage is null at this point. I've entered in valid user credentials, the session object has been created. The currentUser and nextPage objects are not null. nextPage is set to "AdminPage". As soon as I try to step into the "if (errorMessage != null || errorMessage.length() != 0) {" line the exception occurs. Is this a scope issue?
Thanks.
package com.sooperseekrit.app;
import java.util.NoSuchElementException;
import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WORequest;
import com.webobjects.eocontrol.EOQualifier;
import er.extensions.appserver.ERXDirectAction;
import er.extensions.eof.ERXEC;
import com.sooperseekrit.components.AdminPage;
import com.sooperseekrit.components.Main;
import com.sooperseekrit.components.UserPage;
import com.sooperseekrit.model.User;
public class DirectAction extends ERXDirectAction {
public DirectAction(WORequest request) {
super(request);
}
@Override
public WOActionResults defaultAction() {
return pageWithName(Main.class.getName());
}
public WOActionResults loginAction() {
WOComponent nextPage = null;
String username = request().stringFormValueForKey("username");
String password = request().stringFormValueForKey("password");
String errorMessage = null;
if (username == null || username.length() == 0 ||
password == null || password.length() == 0) {
errorMessage = "Please enter a username and password";
} else {
try {
EOQualifier qual = User.USERNAME.eq(username).and(User.PASSWORD.eq(password));
User user = User.fetchRequiredUser(ERXEC.newEditingContext(), qual);
((Session)session()).set_currentUser(user);
if (user.group().name().equals("Admin")) {
nextPage = pageWithName(AdminPage.class.getName());
} else if (user.group().name().equals("User")) {
nextPage = pageWithName(UserPage.class.getName());
}
} catch (NoSuchElementException e) {
errorMessage = "No user found for those credentials";
} catch (Exception e) {
// something bad happened
}
}
if (errorMessage != null || errorMessage.length() != 0) {
nextPage = pageWithName(Main.class.getName());
nextPage.takeValueForKey(errorMessage, "errorMessage");
nextPage.takeValueForKey(username, "username");
nextPage.takeValueForKey(password, "password");
}
return nextPage;
}
}