Reputation: 15357
I have this code (which is called from paintComponent
in a class that inherits from JPanel
.
assert(red >= 0);
assert(red <= 255);
assert(green >= 0);
assert(green <= 255);
assert(blue >= 0);
assert(blue <= 255);
Color color = new Color(red, green, blue);
After some time I get an exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red
at java.awt.Color.testColorValueRange(Unknown Source)
at java.awt.Color.<init>(Unknown Source)
at java.awt.Color.<init>(Unknown Source)
at display.DrawCanvas.drawLed(DrawCanvas.java:55)
at display.DrawCanvas.paintComponent(DrawCanvas.java:36)
at javax.swing.JComponent.paint(Unknown Source)
DrawCanvas.java:55 (line 55) is the last line in the code fragment above.
How is it possible that an argument is out of range while all values are between 0 and 255 (included)?
Upvotes: 0
Views: 319
Reputation: 178253
Java assertions aren't enabled by default.
From the command line, pass the -ea
command line flag to enable assertions.
java -ea your.main.ClassHere
From Eclipse, you'll have to go to your particular Run Configuration and add in -ea
in your "VM Arguments" as described here.
From IntelliJ, it's similar. In your particular Run Configuration, add in -ea
in "VM options".
Regardless of your IDE, the goal is to get it to add -ea
after java
and before your main class name to enable assertions.
Once you've got assertions enabled, then they will stop your program with an AssertionError
before you get the IllegalArgumentException
.
This particular case will work since you've indicated in comments that the value is an int
-- 267
. Note that if you happen to have float
values, then they could pass the assertions with invalid values that are greater than 1.0f
and less than or equal to 255.0f
.
Upvotes: 1