访问由 valueof(0) 初始化的 BigDecimal 时获取 java.lang.NoSuchMethodError
Getting java.lang.NoSuchMethodError when accessing BigDecimal initialized by valueof(0)
我有一个 class User
,BigDecimal 字段是私有的,我正在尝试通过 getter.
访问它
但是当我尝试访问余额时,我得到了 NoSuchMethodError
@Service
public class PaymentProcessor {
@Autowired
UserDaoService userDao;
public void withdrawFunds(String payerId, double amount){
User payer = userDao.getByUserName(payerId);
if ( payer.getBalance().doubleValue() // I get Error here
< amount){
throw new RuntimeException("Insufficient Funds");
}
else {
payer.updateBalance(BigDecimal.valueOf(amount * (-1)));
userDao.saveUser(payer);
}
}
}
编辑用户 CLASS 添加
@Entity
@Table(name = "users")
public class User {
@Id
private String username;
private String email;
private String name;
private String password;
private BigDecimal balance;
private String personalInfo;
private int rating;
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
}, fetch = FetchType.EAGER)
@JoinTable(name = "user_role",
joinColumns = @JoinColumn(name = "username"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<UserRole> userRoles = new HashSet<>();
public User() {
userRoles.add(new UserRole("default"));
this.rating = 0;
this.balance = BigDecimal.valueOf(0);
}
public void updateBalance(BigDecimal balance) {
this.balance = this.balance.add(balance);
}
public BigDecimal getBalance() {
return balance;
}
将您的私有变量更改为:private BigDecimal balance = BigDecimal.ZERO;
发生错误,因为在这一行 User payer = userDao.getByUserName(payerId);
中,您从 User
调用默认构造函数,其中 balance
为空值
public class User() {
super()
.....
this.balance = null;
....
}
我有一个 class User
,BigDecimal 字段是私有的,我正在尝试通过 getter.
但是当我尝试访问余额时,我得到了 NoSuchMethodError
@Service
public class PaymentProcessor {
@Autowired
UserDaoService userDao;
public void withdrawFunds(String payerId, double amount){
User payer = userDao.getByUserName(payerId);
if ( payer.getBalance().doubleValue() // I get Error here
< amount){
throw new RuntimeException("Insufficient Funds");
}
else {
payer.updateBalance(BigDecimal.valueOf(amount * (-1)));
userDao.saveUser(payer);
}
}
}
编辑用户 CLASS 添加
@Entity
@Table(name = "users")
public class User {
@Id
private String username;
private String email;
private String name;
private String password;
private BigDecimal balance;
private String personalInfo;
private int rating;
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
}, fetch = FetchType.EAGER)
@JoinTable(name = "user_role",
joinColumns = @JoinColumn(name = "username"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<UserRole> userRoles = new HashSet<>();
public User() {
userRoles.add(new UserRole("default"));
this.rating = 0;
this.balance = BigDecimal.valueOf(0);
}
public void updateBalance(BigDecimal balance) {
this.balance = this.balance.add(balance);
}
public BigDecimal getBalance() {
return balance;
}
将您的私有变量更改为:private BigDecimal balance = BigDecimal.ZERO;
发生错误,因为在这一行 User payer = userDao.getByUserName(payerId);
中,您从 User
调用默认构造函数,其中 balance
为空值
public class User() {
super()
.....
this.balance = null;
....
}