'ActionContext.getContext() returns null using Session in Struts 2

I am trying to use Session in Struts 2:

private Map<String, Object> session = ActionContext.getContext().getSession();

But I'm getting this error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke 
"com.opensymphony.xwork2.ActionContext.getSession()" because the return value of 
"com.opensymphony.xwork2.ActionContext.getContext()" is null

I tried to use SessionMap of Struts but still got the same error message. I have tried changing the struts-core libraries and also tried a few solutions from Google but still doesn't solve this issue.



Solution 1:[1]

ActionContext is a container placeholder for the action invocation, more detailed explanation is here.

There's a lot of reasons when an action context could be null. You are starting another thread, or modify existing one, or it's just not created yet.

To get a SessionMap there's another approach. Let your action class to implement SessionAware and make sure you have configured a default interceptor stack for the action. After that the session map should be injected to the action instance. More about it here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roman C