In eclipse go on Run Configurations, arguments and set -ea
into VM arguments
-ea
options mean enable assertions, without it the assertions are ignored
Domanda
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.
Soluzione 2
In eclipse go on Run Configurations, arguments and set -ea
into VM arguments
-ea
options mean enable assertions, without it the assertions are ignored
Altri suggerimenti
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