Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4703

NullPointerException in ActionSupport when use ModelDriven

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.5
    • Fix Version/s: 2.5.8
    • Component/s: Core
    • Labels:
      None
    • Environment:

      Windows 7
      WebSphere 8.5

      Description

      When we use getText method in a ModelDriven's bean. We got NullPointerException in ActionSupport. We found the variable "container" is null when the bean is got from ModelDriven.getModel()

      Following is the exception stack:

      java.lang.NullPointerException
      	at com.opensymphony.xwork2.ActionSupport.getLocale(ActionSupport.java:68)
      	at com.opensymphony.xwork2.TextProviderSupport.getLocale(TextProviderSupport.java:329)
      	at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:204)
      	at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:119)
      	at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:86)
      

      Note: we are upgrading the project from 2.3.24.3 to 2.5.5. In version 2.3.24.3 doesn't have this issue.

        Activity

        Hide
        hudson Hudson added a comment -

        SUCCESS: Integrated in Jenkins build Struts-JDK7-master #544 (See https://builds.apache.org/job/Struts-JDK7-master/544/)
        WW-4703 Increases container field visibility (lukaszlenart: rev 24a97d33e68ddc44b3d6e3d77bf2be353c3c655a)

        • (edit) core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
        Show
        hudson Hudson added a comment - SUCCESS: Integrated in Jenkins build Struts-JDK7-master #544 (See https://builds.apache.org/job/Struts-JDK7-master/544/ ) WW-4703 Increases container field visibility (lukaszlenart: rev 24a97d33e68ddc44b3d6e3d77bf2be353c3c655a) (edit) core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
        Hide
        billychk billy added a comment -

        Thanks for your help

        Show
        billychk billy added a comment - Thanks for your help
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        And using ActionSupport for non-action classes is a bad idea.

        Show
        lukaszlenart Lukasz Lenart added a comment - And using ActionSupport for non-action classes is a bad idea.
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Like this

        public class MyAction extends ActionSupport implements ModelDriven, SessionAware, ServletRequestAware {
        {
            private Map<String, Object> session;
            private HttpServletRequest request;
            private MyForm myForm;
        
            private Container container;
        
            @Inject
            public void setContainer(Container container) {
                this.container = container;
            }
        
            ....
        

        Struts has it's own DI.

        Show
        lukaszlenart Lukasz Lenart added a comment - Like this public class MyAction extends ActionSupport implements ModelDriven, SessionAware, ServletRequestAware { { private Map< String , Object > session; private HttpServletRequest request; private MyForm myForm; private Container container; @Inject public void setContainer(Container container) { this .container = container; } .... Struts has it's own DI.
        Hide
        billychk billy added a comment -

        I know this information. I doesn't get what you mean "For now you can define a setter like this [2] and container field in your action" <-- how to do this?

        Can you tell some practical example?

        I have mentioned that it worked at 2.3.24.3

        Show
        billychk billy added a comment - I know this information. I doesn't get what you mean "For now you can define a setter like this [2] and container field in your action" <-- how to do this? Can you tell some practical example? I have mentioned that it worked at 2.3.24.3
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 24a97d33e68ddc44b3d6e3d77bf2be353c3c655a in struts's branch refs/heads/master from Lukasz Lenart
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=24a97d3 ]

        WW-4703 Increases container field visibility

        Show
        jira-bot ASF subversion and git services added a comment - Commit 24a97d33e68ddc44b3d6e3d77bf2be353c3c655a in struts's branch refs/heads/master from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=24a97d3 ] WW-4703 Increases container field visibility
        Hide
        billychk billy added a comment -

        problem not fixed

        Show
        billychk billy added a comment - problem not fixed
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Ah... container is private [1] :\ I will increase its visibility. For now you can define a setter like this [2] and container field in your action

        [1] https://github.com/apache/struts/blob/master/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java#L40
        [2] https://github.com/apache/struts/blob/master/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java#L290-L293

        Show
        lukaszlenart Lukasz Lenart added a comment - Ah... container is private [1] :\ I will increase its visibility. For now you can define a setter like this [2] and container field in your action [1] https://github.com/apache/struts/blob/master/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java#L40 [2] https://github.com/apache/struts/blob/master/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java#L290-L293
        Hide
        billychk billy added a comment -

        How to do "container.inject(myForm)" ? Can you tell me more about it?

        Do you have any example? If possible, Could you mind to modify my example?

        Show
        billychk billy added a comment - How to do "container.inject(myForm)" ? Can you tell me more about it? Do you have any example? If possible, Could you mind to modify my example?
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        You created MyForm manually that's why it doesn't work. If you want to use ActionSupport in this way which is bit awkward, you must inject required dependencies via container.inject(myForm)

        Show
        lukaszlenart Lukasz Lenart added a comment - You created MyForm manually that's why it doesn't work. If you want to use ActionSupport in this way which is bit awkward, you must inject required dependencies via container.inject(myForm)
        Hide
        billychk billy added a comment - - edited

        Here is my example, I have simplified my business logic.

        public class MyAction extends ActionSupport implements ModelDriven, SessionAware, ServletRequestAware {
        {
            private Map<String, Object> session;
            private HttpServletRequest request;
            private MyForm myForm;
        
            public void setSession(Map<String, Object> session){
                this.session = session;
            }
            
            public void setServletRequest(HttpServletRequest request) {
        		this.request = request;
            }
        	
        	public MyForm getModel() {
            	myForm = new MyForm();
        		return myForm;
        	}
        	
        	 public String execute() {
        	    myForm.doSomething();
        		session.put("MyForm", myForm);
        		return "success";
        	
        	}
        	
        }
        
        
        public class MyForm extends ActionSupport {
        private String myLabel;
        	public void doSomething(){
        		myLabel =  getText("my.label.01");
        	}
        
        }
        
        Show
        billychk billy added a comment - - edited Here is my example, I have simplified my business logic. public class MyAction extends ActionSupport implements ModelDriven, SessionAware, ServletRequestAware { { private Map< String , Object > session; private HttpServletRequest request; private MyForm myForm; public void setSession(Map< String , Object > session){ this .session = session; } public void setServletRequest(HttpServletRequest request) { this .request = request; } public MyForm getModel() { myForm = new MyForm(); return myForm; } public String execute() { myForm.doSomething(); session.put( "MyForm" , myForm); return "success" ; } } public class MyForm extends ActionSupport { private String myLabel; public void doSomething(){ myLabel = getText( "my.label.01" ); } }
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Can you post an example code of your ModelDriven action and how do you use getText?

        Show
        lukaszlenart Lukasz Lenart added a comment - Can you post an example code of your ModelDriven action and how do you use getText ?

          People

          • Assignee:
            Unassigned
            Reporter:
            billychk billy
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development