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

NullPointer in com.opensymphony.xwork2.ActionSupport.getLocale

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Not A Problem
    • Affects Version/s: 2.5.10
    • Fix Version/s: 2.5.12
    • Component/s: Core
    • Labels:
      None

      Description

      After upgrading from 2.5.2 to 2.5.10 I get a NullPointerException when calling getText("myKey") in an utility class like this:

      Example
      public class ListGenerator extends ActionSupport {
      
          public final List<String> getValues() {
              final List<String> result = new ArrayList<>();
      
              result.add(getText("select.header"));
              result.add(getText("register.female"));
              result.add(getText("register.male"));
      
              return result;
          }
      }
      

      java.lang.NullPointerException at com.opensymphony.xwork2.ActionSupport.getLocale(ActionSupport.java:64)

      ....

      ERROR org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler - Exception occurred during processing request: java.lang.NullPointerException

        Activity

        Hide
        flyingfischer Markus Fischer added a comment -

        The problem appears when calling the utility class from another class like this:

        ListGenerator generator = new ListGenerator();
        generator.getValues();
        
        Show
        flyingfischer Markus Fischer added a comment - The problem appears when calling the utility class from another class like this: ListGenerator generator = new ListGenerator(); generator.getValues();
        Hide
        aleksandr-m Aleksandr Mashchenko added a comment -

        When you're creating a new instance of the action by yourself the DI won't work and needed components won't be injected.

        If you really want to get localized message through Struts2 try LocalizedTextUtil, otherwise just load resource bundle and used it.

        Show
        aleksandr-m Aleksandr Mashchenko added a comment - When you're creating a new instance of the action by yourself the DI won't work and needed components won't be injected. If you really want to get localized message through Struts2 try LocalizedTextUtil , otherwise just load resource bundle and used it.
        Hide
        flyingfischer Markus Fischer added a comment -

        Thanks! That's it...

        The behaviour changed between 2.5.2 and 2.5.10. But this is not an issue and can be easily fixed in the project. Done!

        Show
        flyingfischer Markus Fischer added a comment - Thanks! That's it... The behaviour changed between 2.5.2 and 2.5.10. But this is not an issue and can be easily fixed in the project. Done!
        Hide
        flyingfischer Markus Fischer added a comment -

        Not a problem, closed and thanks!

        Show
        flyingfischer Markus Fischer added a comment - Not a problem, closed and thanks!
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Sorry for that but I want to use DI as much as possible to get rid of those static methods flying around. You can always use container to inject dependencies, in an action it can be like this:

        public String execute() throws Exception {
            ListGenerator generator = container.inject(ListGenerator.class);
            ....
        }
        
        Show
        lukaszlenart Lukasz Lenart added a comment - Sorry for that but I want to use DI as much as possible to get rid of those static methods flying around. You can always use container to inject dependencies, in an action it can be like this: public String execute() throws Exception { ListGenerator generator = container.inject(ListGenerator.class); .... }

          People

          • Assignee:
            Unassigned
            Reporter:
            flyingfischer Markus Fischer
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development