2 Replies Latest reply on Dec 5, 2003 4:26 PM by john_anderson_ii

    Chasing a NullPointerException

    john_anderson_ii

      I'm a newb to j2ee, so I'm struggling to complete all the excercises in Hightower's CMP tutorial. For the most part I follow his examples, but I've changed it around some so I can use jsps and servlets as clients instead of console apps.

      The null pointer starts in a method in a bean called UserFacade that goes like.

      public void addRole(String email, String roleName){
      try{
      UserInfoLocal user = localHome.findByPrimaryKey(email);
      RoleBeanLocal = roleHome.findByPrimaryKey(roleName);
      Collection roles = user.getRoles();
      Boolean b = roles.add(roleName);
      }catch(FinderException e){
      throw new EJBException("Could not bind user to role", e);
      }

      Now, I even know why this method is throwing a null pointer exception. When I debug the running code with breakpoints, I see that even after Collection roles = user.getRoles roles = null! Does an empty collection = null? Or, is roles not even a Collection at all. After a little re-coding and re-running I found out that any of the methods inherent to Collection will cause a null pointer exception, even roles.isEmpty(). The method getRoles() comes from an abstract CMP entity bean. So the methods are defined as

      public abstract class UserInfo implements EntityBean{

      ejbCreate(......){
      ...
      }
      public abstract java.util.Collection getRoles();
      public abstract void setRoles(Collection roles);
      }

      I am using a mysql database, and I have no mapping for java.util.Collection. So the field type for Roles[/] that is created when the package is deployed is LONGBLOB.

      When a row in the UserInfo table that Roles is a member of is first created, no role information need be specified. Therefore the field Role in any given row may be null. Is there anyway that the container is somehow returning this null value when I try to get the roles of a user that doesn't have any roles defined yet? If so, how can I get JBoss to return an empty Collection instead?

      I've attached my entire source tree, my standardjbosscmp-jdbc.xml and a deployable .ear if anyone would like to take a closer look at this problem.

      Ok, maybe I haven't attached my stuff. That doesn't seem to be working.