gpt4 book ai didi

java - java中断言后出现NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:43:33 26 4
gpt4 key购买 nike

以下方法是名为election_date 的字段的 setter ,该字段的类型为java.util.date。它是 tomcat 应用程序的一部分。

执行后,我收到一个空指针异常:

    System.out.println("ELECTION DATE: " + this.election_date.toString());
<小时/>
public void setElection_date(Date election_date) {

this.election_date = election_date;
assert (this.election_date != null);
assert (this.election_date.toString()) != null;
System.out.println("ELECTION DATE: " + this.election_date.toString());
}
<小时/>

编辑:请不要告诉我它正在被垃圾收集。我可能稍微偏离巴尔默峰,但也不是那么远。

最佳答案

默认情况下禁用断言。你的变量election_datenull .

添加-eaJAVA_OPTS在运行 Tomcat 之前。

Programming With Assertions: Enabling and Disabling Assertions

By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions.

To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch.

另请参阅问题:How can I enable java assertions in Tomcat

关于java - java中断言后出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20919223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com