2 Replies Latest reply on Nov 11, 2011 1:40 AM by Assen Todorov

    Instance.isUnsatisfied() returns false, but Instance.get() returns NULL.Developer mistake or bug in Weld?

    Assen Todorov Newbie

      Hi @all,


      I have the following producer :


      public class LanguageProducer
      {
      
         ...
      
         @Inject
         private HttpServletResponse httpResponse;
      
         @Inject
         @CookieParam(COOKIE_NAME_LANGUAGE)
         private Instance<String> languageResolver;
      
         @Inject
         @Authenticated
         private Instance<User> currentUserResolver;
      
         private String currentLanguage = null;
      
         @Produces
         @Named
         @CurrentLanguage
         public String getCurrentLanguage()
         {
            if (currentLanguage != null)
            {
               return currentLanguage;
            }
      
            if (!currentUserResolver.isUnsatisfied())
            {
               currentLanguage = currentUserResolver.get().getLanguage();
               return currentLanguage;
            }
      
            if (!languageResolver.isUnsatisfied())
            {
               currentLanguage = languageResolver.get();
               return currentLanguage;
            }
      
            return DEFAULT_LANGUAGE;
         }
      
         public void languageListener(@Observes @CurrentLanguage final String aLanguage)
         {
            currentLanguage = aLanguage;
            resetUserLanguage(aLanguage);
            resetLanguageCookie(aLanguage);
         }
      }
      
      



      and I try to inject the currentLanguage in another bean like this


      @Inject
      @CurrentLanguage
      private String currentLang;
      



      but then I get NullPointerException and the stacktrace shows that the exception is raised on this row in the producer (the application is deployed without erors) :


      currentLanguage = currentUserResolver.get().getLanguage();
      



      It seems that Instance.isUnsatisfied returns false (i.e. there exists a bean with the required qualifier and type), but Instance.get() returns NULL.


      Is it developer mistake or bug?
      I need your help.


      PS
      User is a JPA entity and beans.xml is present in META-INF of all the jars with CDI beans.