0 Replies Latest reply on Jul 1, 2013 2:30 PM by sermilion

    EJB EntityManager results in NullPointerException

    sermilion

      I have an error that I cannot logically understand. I am implementing RSS processing app. The logic is that user takes link of some RSS feed and adds to database. If the feed already exist in database, it takes the id of feed and adds it to subscriptions of the user. Here is the code:

       

           if (existfeed == null) {
                  feed = loadFeed(url, syndfeed);
                  em.persist(feed);
                  if (feed.getFeedId() == null) {
                      return "Persist of feed failed";
                  }
                      uf.setFeedId(feed);
                      uf.setUserId(user_id);
                      try {
                          em.persist(uf);
                          loadFeedItems(syndfeed, feed);
                          //boolean ufadded = uf.addUserFeed(em);
                      } catch (NoResultException ex) {
                          return "Persist of userfeed failed";
                      } catch (PersistenceException ex) {
                          return "Persist of userfeed failed, em";
                      }
                      //boolean ufadded = uf.addUserFeed(em);

                      //return true;               
                  return "Feed added";
              } else {
                  dbFeed = getFeed(feed.getLink());
                  if(dbFeed==null)
                      return "Faile to get feed from database";
                  //http://appleinsider.ru/feed
                  uf.setFeedId(dbFeed);
                  uf.setUserId(user_id);
                  try {
                      em.persist(uf);
                  } catch (PersistenceException ex) {
                      //System.out.println(ex.getMessage());
                      return "Unable to persist userfeed";
                  }
                  return "Feed added, existing";
              }

       

      However, if user added a feed, another user cannot add it and there is the error:

       

           Caused by: java.lang.NullPointerException
      at ejb.FeedBean.addNewFeed(FeedBean.java:203) [Group_7_Impl_uwx09sua-ejb.jar:]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [classes.jar:1.6.0_51]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [classes.jar:1.6.0_51]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_51]
      at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_51]
      at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory

       

       

      I just dont see why would it fail. One of my opinion is that possibly, EntityManager somehow is null. But, in first part of if statement it works.

      I tried to use EntityManager find method, it resulted same error. So, definitely, EntityManager is somehow null in second part of if statement.

      Thanx for all the help.