getting null pointer exception while testing xwork action class

334 Views Asked by At

I'm trying to run xwork alone i.e using main method() but unfortunately I'm not getting my action class executed, instead I'm getting null pointer exception as my xwork container inside defaultActionFactory is null. Here is my action class:

import com.opensymphony.xwork2.Action;

public class HelloWorld implements Action {
String name;
String greeting;
public void setName(String name) {
this.name = name;
}
public String getGreeting() {
return greeting;
}
public String execute() {
greeting = "Hello, " + name;
return SUCCESS;
}
}

My Main class for testing this action is as below:

public class Main {
public static void main(String[] args) throws Exception {
Map params = new HashMap();
params.put("name", "Patrick");
Map extraContext = new HashMap();
extraContext.put(ActionContext.PARAMETERS, params);

ActionProxyFactory factory = new DefaultActionProxyFactory();
ActionProxy proxy =
factory.createActionProxy("", "hello", extraContext);
System.out.println(proxy.execute());
HelloWorld hello = (HelloWorld) proxy.getAction();
System.out.println(hello.getGreeting());
}
}

Lastly, I put my xwork.xml in the same package folder, below is my xwork.xml file

<!DOCTYPE xwork PUBLIC “-//OpenSymphony Group//XWork 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<package name="default">
<action name="hello"
class="org.arpit.javapostsForLearning.HelloWorld">
</action>
</package>
</xwork>

I'm stuck here, i did lots of search but could not find anything fruitful,Please let me know what is missing here? help would be much appreciated

null pointer is happening in below code of DefaultActionProxyFactory but I'm sending everything from my main class, it says container is null

 public ActionProxy createActionProxy(ActionInvocation inv, String namespace, String actionName, String methodName, boolean executeResult, boolean cleanupContext)
   {
    DefaultActionProxy proxy = new DefaultActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
    this.container.inject(proxy);
     proxy.prepare();
     return proxy;
   }
1

There are 1 best solutions below

2
jbytecode On

Since I do not know about the test framework that you use, however, I can give an idea. In your class file, give default values for name and greeting variables and observe whether they change over the runtime (or see which one changes).