为了账号安全,请及时绑定邮箱和手机立即绑定

服务类Spring boot中的空指针异常

服务类Spring boot中的空指针异常

GCT1015 2021-10-28 14:17:45
美好的一天.. 我正在尝试创建一个 spring boot web 应用程序,在其中我必须在 spring boot 的用户详细信息中找到用户,但是在使用测试时出现错误,这是我的配置:用户类:import java.io.Serializable;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name = "USERS")public class User implements Serializable {  /**     *      */    private static final long serialVersionUID = 1L;  @Id  @GeneratedValue(strategy= GenerationType.IDENTITY)  @Column(name = "USERNAME")  private String username;  @Column(name = "PASSWORD", nullable = false)  private String password;  @Column(name = "ENABLED", nullable = false)  private boolean enabled;  public User() {    super();    // TODO Auto-generated constructor stub}public User(String username, String password, boolean enabled, Set<Authorities> authorities, Customer customer) {    super();    this.username = username;    this.password = password;    this.enabled = enabled;    this.authorities = authorities;    this.customer = customer;}@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")  private Set<Authorities> authorities = new HashSet<>();  @OneToOne(cascade = CascadeType.ALL)    private Customer customer;  //Getter and Setter methodspublic String getUsername() {    return username;}public void setUsername(String username) {    this.username = username;}public String getPassword() {    return password;}public void setPassword(String password) {    this.password = password;}public boolean isEnabled() {    return enabled;}public void setEnabled(boolean enabled) {    this.enabled = enabled;}public Set<Authorities> getAuthorities() {    return authorities;}public void setAuthorities(Set<Authorities> authorities) {    this.authorities = authorities;}
查看完整描述

1 回答

?
皈依舞

TA贡献1851条经验 获得超3个赞

您的userService字段是null因为您尚未为其分配值。您的测试似乎没有使用您的userDetailsService字段,因此您可以将其删除,然后替换此行:

userDetailsService = ctx.getBean(UserDetailsService.class);

有了这个:

userService = ctx.getBean(UserService.class);


查看完整回答
反对 回复 2021-10-28
  • 1 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信