首页 > 解决方案 > Java:空检查后的空指针异常

问题描述

有人能帮我理解如何在这里获得空指针异常吗?

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> grantedAuthorityCollection = new ArrayList<>();
        Collection<UserRole> userRoleCollection = user.getUserRoleList();
        if(userRoleCollection != null) {
            if (userDetailsService.getAuthorities(userRoleCollection) != null) {
            // ^^^ it occurs at the line above ^^^
                grantedAuthorityCollection.addAll(userDetailsService.getAuthorities(userRoleCollection));
            }
        }
        return grantedAuthorityCollection;
    }

这是我在运行时得到的:

java.lang.NullPointerException: null
    at ua.tucha.passpass.web.security.UserDetails.getAuthorities(UserDetails.java:31)
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.createSuccessAuthentication(AbstractUserDetailsAuthenticationProvider.java:225)
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.createSuccessAuthentication(DaoAuthenticationProvider.java:137)
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:197)
    ...

怎么会这样?我刚刚确定如果没有nullin userRoleCollection,如果有 ,就不会执行下一条指令,对null吗?

非常感谢您的帮助。

标签: javanullpointerexception

解决方案


推荐阅读