5 Replies Latest reply on Feb 1, 2008 5:30 PM by nickarls

    NPE when logging inside a property getter

    cyberanto

      I get a Nullpointer Exception when I add a log statement to my property getter. Example:

      ---------------------------------------------------
      @Name("editDataView")
      public class EditDataViewAction ...

      skuSelectionAction() {
      ...
      Contexts.getSessionContext().set("skuModel", selectedSku);
      ...
      }

      ----------------------------------------------------
      @Scope(SESSION)
      @Name("skuModel")
      public class SkuModel ...

      @Logger private Log log;

      private List features = new ArrayList();

      // lazy loading
      public List getFeatures() {
      if (features.size() < 1) {
      log.info("features.size() was smaller than 1");
      try {
      features.addAll(featureDao.getFeaturesByFeatureListId(featureListId, this));
      } catch (ServerException e) {
      e.printStackTrace();
      }
      }
      return features;
      }

      Caused by: java.lang.NullPointerException
      at com....model.SkuModel.getFeatures(SkuModel.java:196)
      at com....view.EditDataViewAction.skuSelectionAction(EditDataViewAction.java:53)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

      If I take out the log statement, this works just fine ... looks like the Logger has not been initialized at reflection time?

      Seam version: 2.0.1.CR1

        • 1. Re: NPE when logging inside a property getter
          pmuir

          Where do you call getFeatures from?

          • 2. Re: NPE when logging inside a property getter

            Also make sure that your Log is a seam Log, and not a commons log or something different. check your import.s

            • 3. Re: NPE when logging inside a property getter
              cyberanto

              hmm, I had transferred a couple of DAOs from another project which had been using commons logging.

              After replacing all my commons logging imports with seam.log.Log, all hell is breaking loose ...

              e.g.

              ==========================================

              import org.jboss.seam.annotations.Logger;
              import org.jboss.seam.log.Log;

              @Name("applicationCache")
              @Scope(APPLICATION)
              public class SwcmApplicationCache implements Serializable {

              @Logger private Log log;

              CategoryDAO categoryDao = new CategoryDAO();

              public String refreshCategories() {
              String outcome = ActionOutcomes.SUCCESS;

              try {
              categories.clear();
              categories.addAll(categoryDao.getAllCategories());
              staleCategory = false;
              } catch (ServerException e) {
              e.printStackTrace();
              outcome = ActionOutcomes.FAILURE;
              }

              log.info("updating application cache categories outcome: "+outcome);
              return outcome;
              }

              ...
              }

              ============================================

              @Name("categoryDao")
              public class CategoryDAO extends AbstractDAO {

              @Logger private Log log;

              public ArrayList getAllCategories() throws ServerException {

              ...
              System.out.println("Logger is: "+log);
              log.info("#0 categories returned ...", categories.size());
              return categories;
              }
              }

              bombs on Line 55 - the log.info() in the CategoryDAO ... (categories is NOT null) ...

              14:39:56,171 INFO [STDOUT] Logger is: null

              Caused by: java.lang.NullPointerException
              at com.jostens.swcm.sql.CategoryDAO.getAllCategories(CategoryDAO.java:55)
              at com.jostens.swcm.application.SwcmApplicationCache.refreshCategories(SwcmApplicationCache.java:68)


              Unless someone can tell me what I am doing wrong, I may have to replace all my logging with System.out.print()'s ...


              I am using JBOSS Developer Studio and set up my project using its ejb wizard. The SEAM Logger works fine in some other classes ...

              • 4. Re: NPE when logging inside a property getter
                cyberanto

                I found out what my mistake was:

                I didn't use injection to create the component which contained the SEAM Logger ...

                injecting the component initializes the Logger correctly.

                @In(create=true)
                private CategoryDAO categoryDao;

                //
                // CategoryDAO categoryDao = new CategoryDAO();

                There used to be a class org.jboss.seam.log.LogImpl ... not any more

                How do I get a Seam Logger if I want to instantiate the class "normally" ?

                • 5. Re: NPE when logging inside a property getter
                  nickarls

                  Haven't used it but Logger seams to have some static methods that might be interesting.