2

I have a hyperlink named "Click for new User". Once I click the link, I got NullPointerException instead of opening RegisterPage.jsp page.

I post my code here, I can't find my mistake

index.jsp:

<s:form action="verify">
<s:textfield name="username" key="label.uname"  required="true"/>
<s:password name="password" key="label.pass" required="true"/>
<s:submit value="Login" align="center"/> 
</s:form>
<a href="<s:url action='register'/>">Click for New User</a>

struts.xml:

<struts>
<package name="a" extends="struts-default">
<action name="verify" class="java4s.Login"  method="execute">
<result name="success">loginsuccess.jsp</result>
<result name="error">error.jsp</result>
<result name="input">index.jsp</result>
</action>
<action name="register" class="java4s.Login" method="register">
<result name="success">RegisterPage.jsp</result>
</action>
</package>
</struts>

Login.java(Action Class):

package java4s;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {

private static final long serialVersionUID = 1L;
private String username,password;
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String register(){
    System.out.println("register ()");
    return SUCCESS;
}
public String execute(){
    if (getUsername().equals("java4s")&&getPassword().equals("java4s")) {
        return SUCCESS;
    } else {
        this.addActionError(getText("u.p.wrong"));
        return ERROR;
    }   
}
@Override
public void validate() {
        if (getUsername().equals("")||getUsername().length()==0)
        this.addFieldError("username",getText("error.uname"));
        if(getPassword().equals("")||getPassword().length()==0) 
        this.addFieldError("password", getText("error.pass"));

}
}

Exception:

 javax.servlet.ServletException: java.lang.NullPointerException
 org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
 org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:4‌​19)
 java.lang.NullPointerException
java4s.Login.validate(Login.java:38)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:200)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) 
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
Roman C
  • 49,761
  • 33
  • 66
  • 176
Krish
  • 304
  • 3
  • 5
  • 19
  • Where is NPE coming from? – Aleksandr M Aug 16 '13 at 08:13
  • @AleksandrM Perhaps it come from struts.xml file,i post my exception here javax.servlet.ServletException: java.lang.NullPointerException org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515) org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419) – Krish Aug 16 '13 at 08:16
  • 1
    I have added your exception to your post. In the future please add all relevant details to your question and not as unformatted comments. Further, what's with the unindented XML? It's not exactly easy to read. And finally, that isn't the full stacktrace. Please post the **full** stacktrace. – Boris the Spider Aug 16 '13 at 08:22
  • Which version of Struts2? – Aleksandr M Aug 16 '13 at 08:23
  • @AleksandrM struts2.0 – Krish Aug 16 '13 at 08:27
  • Look at the jar file (e.g. struts2-core-x.x.x.jar). What is x.x.x in your case? – Aleksandr M Aug 16 '13 at 08:33
  • @AleksandrM struts2-core-2.0.11.jar – Krish Aug 16 '13 at 08:35
  • Why are you using such old version? Current latest is 2.3.15.1. – Aleksandr M Aug 16 '13 at 08:41
  • yeah ur right,But currently i doing project in struts2.0 next project,i will move on latest version of the struts – Krish Aug 16 '13 at 08:48
  • Do you have `Login` class in `java4s` package? – Aleksandr M Aug 16 '13 at 09:02
  • can you provide more log information or is it only part being shown by server? and what page/message you are getting when clicking on link? are you getting server error message page? – Umesh Awasthi Aug 16 '13 at 09:05
  • @AleksandrM yes,i having Class in java4s.Login – Krish Aug 16 '13 at 09:07
  • @UmeshAwasthi i post the Error message,please look at my post... – Krish Aug 16 '13 at 09:10
  • NPE `java4s.Login.validate(Login.java:38)`. – Aleksandr M Aug 16 '13 at 09:14
  • `java4s.Login.validate(Login.java:38)`, can you also show your action class? are you doing nay sort of validation there? – Umesh Awasthi Aug 16 '13 at 09:14
  • @Krish: Could you post your `Login` class `validate` method. – Aleksandr M Aug 16 '13 at 09:21
  • @AleksandrM please look at my post,i post my Action Class(Login) – Krish Aug 16 '13 at 09:25
  • 2
    when you are calling your action by link, there are no username and password field and you are doing validation over them by checking there length and empty value which is causing this issue, since your validate method is being called due to interceptor stack configured by S2 – Umesh Awasthi Aug 16 '13 at 09:26
  • And not checking for null. – Aleksandr M Aug 16 '13 at 09:27
  • @UmeshAwasthi please look at my post,i post my Action Class(Login) – Krish Aug 16 '13 at 09:27
  • 1
    i will suggest you to use Apache common library for all such case and it's String Util method `StringUtils.isBlank()` is null safe check which will be able to solve your issue and more over common is really a helpful API in many aspects – Umesh Awasthi Aug 16 '13 at 09:30
  • @UmeshAwasthi is there is any way to segregate the field and link? – Krish Aug 16 '13 at 09:31
  • 1
    I am not sure why you are using validate method while you can either use build in validation framework which is more flexible and provide more details, also as suggested validate method will be triggered each time you action's method is being called due to default stack configured, so either you can create a separate action for registration or u can check for null values in your action class – Umesh Awasthi Aug 16 '13 at 09:33
  • @UmeshAwasthi in my action class i set a condition to check the empty string in validate() please look at my code. – Krish Aug 16 '13 at 09:37
  • 1
    that will throw a null pointer exception when your username is not being set as in that case it will be null and performing any action over null value will throw NPE. what i said that `StringUtils.isBlank()` is null safe which means it will first check if given value is not null and only after that it will check if string is empty or not. – Umesh Awasthi Aug 16 '13 at 09:40
  • 1
    @Krish: Read this: http://struts.apache.org/development/2.x/docs/validation.html. – Aleksandr M Aug 16 '13 at 09:50
  • @AleksandrM i worked in declarative validation(XML validation),now i try in programatic validation(Manual validation). – Krish Aug 16 '13 at 09:57

1 Answers1

0

When you click on hyperlink you don't supply the request URL with parameters username and password (the parameters aren't set by the params interceptor), so the values of this properties are null but the validate() method is called before the action is executed because your action extend ActionSupport which implemented Validateable and ValidationAware that allows the validation interceptor (which is a part of the defaultStack) execute validate() method where you could perform manual checks and provide ActionErrors, FieldErrors, and ActionMessages. But there's no reason to do so if you don't provide the values for the properties. You should either submit the form or set the parameters in the url like

<a id="myLink" href="#">Click for New User</a>
<script type="text/javascript>
$(document).ready(function(){
  $('#myLink').click(function(e) {
    window.location="<s:url action='register'/>"+"?username="+$('username').val()+"&password="+$('password').val();
    return false;
  });
});
</script>

But the problem is the parameter values (username and password) are sensitive information and shouldn't pass in the URL. And you should use either Ajax post or submit the form with the POST request like in this example.

When using manual validations very useful to utilize the Apache commons validator

if (GenericValidator.isBlankOrNull(value))
  addFieldError("FieldLabel", MessageFormat.format(getText("ErrorKey"),
    getText("FieldLabel"), new Object {}));

The similar code could be done for ActionErrors and/or ActionMessages. Other validators are available in the commons-validator package, or you could use Struts2 validators that is out of this question.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • i am sorry for unaccept ur answer, the problem is my workspace,in my workspace having 12 project,so it could not be properly work,during restarting time only ur code will be work,thanks for reply my post. – Krish Aug 18 '13 at 01:12