Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException after assert in java

The following method is a setter for a field called election_date which is of type java.util.date. It is part of a tomcat application.

Upon execution, I get a Null pointer exception on the line:

    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());
}

EDIT: Please don't tell me it's being garbage collected. I may be slightly off the Balmer Peak, but not that far off.

like image 692
GCon Avatar asked Mar 10 '26 14:03

GCon


1 Answers

Assertions are disabled by default. Your variable election_date is null.

Add -ea to JAVA_OPTS before run 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.

Also look at question: How can I enable java assertions in Tomcat

like image 152
MariuszS Avatar answered Mar 12 '26 04:03

MariuszS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!