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

美好的一天.. 我正在尝试创建一个 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 methods


public 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;

}


GCT1015
浏览 177回答 1
1回答

皈依舞

您的userService字段是null因为您尚未为其分配值。您的测试似乎没有使用您的userDetailsService字段,因此您可以将其删除,然后替换此行:userDetailsService&nbsp;=&nbsp;ctx.getBean(UserDetailsService.class);有了这个:userService&nbsp;=&nbsp;ctx.getBean(UserService.class);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java