0

i have implemented PrimeFaces selectOneMenu through github and PrimeFaces and not clear about why themeservice-bean has been defined as Application scope e.g.

@Named
@ApplicationScoped
public class ThemeService {

    private List<Theme> themes;

    @PostConstruct
    public void init() {
        themes = new ArrayList<>();
        themes.add(new Theme(0, "Nova-Light", "nova-light"));
        themes.add(new Theme(1, "Nova-Dark", "nova-dark"));
        themes.add(new Theme(2, "Nova-Colored", "nova-colored"));
        themes.add(new Theme(3, "Luna-Blue", "luna-blue"));
        themes.add(new Theme(4, "Luna-Amber", "luna-amber"));
        themes.add(new Theme(5, "Luna-Green", "luna-green"));
        themes.add(new Theme(6, "Luna-Pink", "luna-pink"));
        themes.add(new Theme(7, "Omega", "omega"));
    }

    public List<Theme> getThemes() {
        return themes;
    } 
}

in my view, the same bean should be defined as View/Request scoped as the list may need to be populated with different records, but as i change the scope it gives error of

java.lang.NullPointerException
    at org.primefaces.showcase.convert.ThemeConverter.getAsObject(ThemeConverter.java:27)

at return statement in converter

                ThemeService service = (ThemeService) fc.getExternalContext().getApplicationMap().get("themeService");
                return service.getThemes().get(Integer.parseInt(value));

may somebody shed some light on this.

Melloware
  • 7,402
  • 2
  • 26
  • 47
  • Also relevant reading: https://stackoverflow.com/questions/2633112/get-jsf-managed-bean-by-name-in-any-servlet-related-class – Selaron Dec 23 '19 at 15:04
  • Or even better: https://stackoverflow.com/questions/7665673/how-to-inject-ejb-persistencecontext-inject-autowired-etc-in-facesconve – Selaron Dec 23 '19 at 15:08

1 Answers1

0

You try to get a request or view scoped bean from applicationMap which stores application scoped beans only.

Try to replace getApplicationMap by getRequestMap or getViewMap.

Or even better @Inject the themeService into your converter:

How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

Selaron
  • 5,617
  • 4
  • 26
  • 38
  • too quick ... thanx it resolved. i didn't understand the converter fully. – Nasir Abbas Dec 23 '19 at 15:12
  • 1
    @NasirAbbas "it resolved" how did it resolve? And what does "too quick" mean? – Selaron Dec 23 '19 at 15:21
  • And the themeService(applicationScoped) **IS** request from the applicationMap.. Weird – Kukeltje Dec 23 '19 at 18:09
  • @kukeltje OP asked in the question that getting the themeService from applicationMap stops working as soon as the scope of themeService is changed to view scope or request scope. (This is how I got the question tho). – Selaron Dec 23 '19 at 18:33
  • Ok, now I read it again, with your comment I agree to your interpretation, but then the title and posted code are completely unrelated. OP should have posted a view or requestscoped bean that throws the error, not the working code (since the code that **_was_** posted seems fine... – Kukeltje Dec 23 '19 at 18:54
  • @Selaron the response was too quick from ur side and immediately resolved and pointed out my mistake and actually i don't have any idea abt ApplicationMap bcoz i copied the example and implemented it as it is although i myself tried to understand. – Nasir Abbas Dec 23 '19 at 19:03