为什么在模型属性中设置 Calendar.getInstance() 时会出现 NullPointerException?

Why i have a NullPointerException when i set Calendar.getInstance() in a attribute of model?

在我的模型中我有这个:

@Entity
public class Plan {
  ...
  @Temporal(TemporalType.TIMESTAMP)
  @Column(nullable = false)
  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private Calendar creationDate;
  ...

在 eclipse 调试器中,我在这一行中有一个 NullPointerException:

plan.setCreationDate(instance);

可能您的 plan 对象没有实例化...

这将防止异常:

if (plan != null){
    plan.setCreationDate(instance);
}