0

problem occurs at return this.permission.contains(permission);

package sef.module15.activity;

import java.util.HashSet;
import java.util.Set;

public class PermissionList implements Permissable {

    private Set<Permission> permission;

    /**
     * Creates a permission object and provides an identifier for it
     * 
     * @param permissionID
     */

    public PermissionList(String permissionID) {

        if (permissionID == null) {
            this.permission = new HashSet<Permission>();
        }

    }

    public Set<Permission> getPermission() {

        return permission;

    }

    public void removePermission(Permission... permission) {
        this.permission.remove(permission);

    }

    public void setPermission(Permission... permission) {
        if (permission == null) {
            setPermission(permission);
        }

    }

    public boolean isPermissable(Permission permission) {
        return this.permission.contains(permission);
    }
}

here's the permissable.java

package sef.module15.activity;

import java.util.Set;

public interface Permissable {

    public void setPermission(Permission... permission);

    public void removePermission(Permission... permission);

    public Set<Permission> getPermission();

    public boolean isPermissable(Permission permission);
}

and heres permission.java

package sef.module15.activity;

public enum Permission {
    READ,
    WRITE,
    CREATE,
    DELETE,
    EXECUTE;

}
4
  • 1
    What is the "calling code" i.e., who calls this class and it's methods?
    – PhD
    Jul 7, 2011 at 3:45
  • setPermission looks somewhat recursive if permission is null also.
    – Mr Moose
    Jul 7, 2011 at 3:51
  • updated who calls the class and the methods involved
    – Rebel
    Jul 7, 2011 at 3:52
  • Not enough. WHERE is the concrete class implementing the interface and the actual methods it calls??? The calling flow = stack trace printed when you get an exception! (Your related classes) Update with the actual line of code(s) that calls setPermission or getPermission etc.,
    – PhD
    Jul 7, 2011 at 3:56

4 Answers 4

0

The set is null if permissionID is passed to the program ? That is a problem

  if (permissionID == null) {
        this.permission = new HashSet<Permission>();
    }

Give us the stack trace and the calling code. It is difficult to pinpoint the source of the NPE without this

1
0

If the constructor's permissionID is null, permissionSet is never assigned a value. Thus, if the permissionID is passed, the methods which access permissionSet will all throw a NullPointerException.

0
  1. If permissionID passed in constructor is not-null, then your instance variable permission is never initialised. Hence, pointing to that will give you a NPE.

  2. Your setPermission() implementation is recursive. A recursion that will not end gracefully and will never add anything to your Set.

Possible fixes:

Initialise your Set in constructor like below,

public PermissionList(Permission permission) {
    permissions = new HashSet<Permission>(); // Change your Set name to permissions
    if (permission != null) {
        permissions.add(permission);
    }    
}

Implementation of setPermissions() method,

public void setPermissions(Permission... permissions) {
    for(Permission per : permissions) {
        this.permissions.add(per);
    }
}
1
  • @Rebel: From where, JUnit came into play? Jul 7, 2011 at 6:42
0

thats because you are trying to check a property from a not initialized variable. Try that to control if you permission variable and the argument passed to the function are not null:

public boolean isPermissable(Permission p) {
        boolean exit = false;
        if((permission != null) && (p != null)){
              exit = permission.contains(p);
        }
        return exit;
    }

anyway i recommend you to create an empty constructor for your class, and initialize all the variables

PermissionList(){
    permission = new HashSet();
}

EDIT

I just found a strange thing in your code. Here

public PermissionList(String permissionID) {

        if (permissionID == null) {
            this.permission = new HashSet<Permission>();
        }

    }

You will never initialize your permission property, unless you call the constructor that way: PermissionList myPermission = PermissionList(null), because you dont have an empty constructor like PermissionList myPermission = PermissionList(), and any string that you pass as permissionID will not be null.

Why you use the permissionID argument? Where do you store it?

Thats why you get a NPE wen you call this.permission in your code. You never initilize the permission property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.