DateTime引用变量从不为null但抛出NullPointerException

时间:2017-05-05 10:19:17

标签: hibernate datetime nullpointerexception spring-data jodatime

我有这个实体:

@Entity
@Table( name = "conference" )
public class Conf implements Serializable {

  @Column( name = "start_date" )
  private DateTime                 startDate;

  @Column( name = "complete_date", unique = false, updatable = true )
  private DateTime             completeDate;

  @Column( name = "is_complet", length = 1, updatable = true, columnDefinition = "TINYINT(1)" )
  private Boolean              isComplete;

  public void setIsComplete( Boolean isComplete ) {
   System.out.println( "date is null ?" + this.completeDate == null ); //for verification
    this.isComplete = isComplete;
    this.completeDate = (this.isComplet == true && completeDate == null) ? new DateTime() : null; // null value is never assigned beccause by default completeDate seems to not be null
    this.startDate = this.completeDate != null ? new DateTime() : new DateTime( this.completeDate.getYear(), 1 + this.completeDate.getMonthOfYear(),
                    this.completeDate.getDayOfMonth(),
                    this.completeDate.getHourOfDay(),
                    this.completeDate.getMinuteOfHour() ); // this throws NPE
 }

问题出在上面的setter方法中。事实上,completeDate即使isComplete = true也不会为空,当我这样检查时:System.out.println( "date is null ?" + this.completeDate == null );结果false。但是当我从completeDate调用任何方法时,我得到了一个N​​ullPointerException。奇怪的是,当我查看我的表conference时,complete_date列为空。 即使我为它指定了一个空值:completeDate=null;它也没有任何改变。

有什么问题?我哪里错了?我该怎么解决这个问题?非常感谢

1 个答案:

答案 0 :(得分:0)

我认为你的逻辑落后了。如果日期为空,则打印出来,而不是。因此,实际上,您实际上将其设置为null,然后尝试在其上调用方法。 => NPE