我的帐户控制器中有此代码。
Account account = accountRepository.findByUsername(username);
参数来自接收字符串"henk@gmail.com"
的RESTcontroller参数用户名。它包含“”的原因是因为我在将它发送到控制器之前使用了javascript命令JSON.Stringify(username)。当您尝试对字符串进行字符串化时,它会添加一对额外的“”。
现在回答我的问题。如何使用此参数accountRepository.findByUsername(username)
返回一个帐户(我通过打印检查了这个:)
System.out.println("Is account null? " + account == null);
但是当我调用该帐户的任何方法时(比如toString()),它会给出一个nullpointer异常?它与包含参数henk@gmail.com的那对“”有关,但是为什么这会导致jpa存储库回复这个神秘的帐户对象?
jpa存储库的定义如下:
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
Account findByUsername(String username);
Account findByUsernameAndPassword(String username, String password);
Set<Account> findByAdminIsTrue();
}
帐户对象定义如下:
@Entity
//@JsonIgnoreProperties
//@JsonIdentityInfo(
// generator = ObjectIdGenerators.PropertyGenerator.class,
// property = "id",
// scope = Account.class)
public class Account implements Serializable {
@Id
@GeneratedValue
private Long id;
private String username;
private String password;
private String name;
private boolean admin;
private boolean enabled;
private int remainingStatutoryLeaveHours = 240;
private int remainingNonStatutoryLeaveHours = 60;
@JsonIgnore
@OneToMany(fetch = FetchType.EAGER, mappedBy = "account", cascade = CascadeType.ALL)
List<LeaveQuery> leaveQueryList;
//
@OneToMany(fetch = FetchType.LAZY, mappedBy = "account", cascade = CascadeType.ALL)
List<LaborPeriod> laborperiods = new ArrayList<>();
@OneToOne
private Person person;
@Enumerated
UserRole userRole = UserRole.USER;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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 Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isAdmin() {
return admin;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
public List<LaborPeriod> getLaborperiods() {
return laborperiods;
}
public void setLaborperiods(List<LaborPeriod> laborperiods) {
this.laborperiods = laborperiods;
}
public List<LeaveQuery> getLeaveQueryList() {
return leaveQueryList;
}
public void setLeaveQueryList(List<LeaveQuery> leaveQueryList) {
this.leaveQueryList = leaveQueryList;
}
public int getRemainingStatutoryLeaveHours() {
return remainingStatutoryLeaveHours;
}
public void setRemainingStatutoryLeaveHours(int remainingStatutoryLeaveHours) {
this.remainingStatutoryLeaveHours = remainingStatutoryLeaveHours;
}
public int getRemainingNonStatutoryLeaveHours() {
return remainingNonStatutoryLeaveHours;
}
public void setRemainingNonStatutoryLeaveHours(int remainingNonStatutoryLeaveHours) {
this.remainingNonStatutoryLeaveHours = remainingNonStatutoryLeaveHours;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", admin=" + admin +
", enabled=" + enabled +
", remainingStatutoryLeaveHours=" + remainingStatutoryLeaveHours +
", remainingNonStatutoryLeaveHours=" + remainingNonStatutoryLeaveHours +
", leaveQueryList=" + leaveQueryList +
", laborperiods=" + laborperiods +
", person=" + person +
", userRole=" + userRole +
'}';
}
}
有人有想法吗?
答案 0 :(得分:1)
如果声明:
account.toString();
是NPE的来源,然后只有一个似是而非的解释:account
的值是null
。
你说这句话:
System.out.println("Is account null? " + account == null);
告诉您account
不为空。你猜怎么着。 不是!
+
的优先级高于==
。所以上面的陈述相当于:
System.out.println(("Is account null? " + account) == null);
将打印false
而不管account
的值,因为连接字符串和null
将是非空字符串。
使用以下语句重新运行您的程序:
System.out.println("Is account null? " + (account == null));
然后你可以解决真正的问题......这就是找出为什么account
是null
以及如何解决这个问题。