0

I use this Enum for available user roles types:

public enum Role implements GrantedAuthority {
  ROLE_ADMIN, ROLE_CLIENT;

  public String getAuthority() {
    return name();
  }
}

Full code: Github

But when I try to convert the list using this code:

claims.put("auth", roles.stream()
                        .map(s -> new SimpleGrantedAuthority(s.getAuthority()))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList())
);

I always get NPE when I try to get s.getAuthority(). Do you know how I can fix this issue?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

2

You should remove all the null values with filter before using map.

roles.stream().filter(Objects::nonNull)
              .map(s -> new SimpleGrantedAuthority(s.getAuthority()))
              .collect(Collectors.toList());
Unmitigated
  • 76,500
  • 11
  • 62
  • 80