Issue Details (XML | Word | Printable)

Key: JSCIENCE-86
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: jscience-issues
Reporter: cheremin
Votes: 1
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
jscience

NPE in Unit.alternate

Created: 04/Feb/08 02:55 AM   Updated: 31/Jan/11 02:49 AM
Component/s: www
Affects Version/s: current
Fix Version/s: Version 6.0

Time Tracking:
Not Specified

Environment:

Operating System: All
Platform: All


Issuezilla Id: 86
Tags:
Participants: cheremin and jscience-issues


 Description  « Hide

When I try to create alternate unit for non-standart one, I've got NPE instead
of supposed IllegalArgumentException, of something like it.

Test case:

final Unit<Duration> minute = SI.SECOND.times( 60 );
minute.alternate("minute"); //here i've got NPE

Browsing sources I found the reason:

AlternateUnit(String symbol, Unit<?> parent) {
if (!parent.isStandardUnit())
throw new UnsupportedOperationException(this
+ " is not a standard unit");

it's quote from AlternateUnit constructor. When parent is non-standart unit, it
throws UnsupportedOperationException, but during evaluation of this
+ " is not a standard unit" we've got NPE from this.toString() (since this
currently is not valid – all fields are null). Here is the stack trace

java.lang.NullPointerException
at javax.measure.unit.AlternateUnit.hashCode(AlternateUnit.java:122)
at java.util.HashMap.get(HashMap.java:343)
at javax.measure.unit.UnitFormat$DefaultFormat.nameFor(UnitFormat.java:305)
at javax.measure.unit.UnitFormat$DefaultFormat.format(UnitFormat.java:639)
at javax.measure.unit.UnitFormat.format(UnitFormat.java:187)
at java.text.Format.format(Format.java:133)
at javax.measure.unit.Unit.toString(Unit.java:499)
at java.lang.String.valueOf(String.java:2615)
at java.lang.StringBuilder.append(StringBuilder.java:116)
at javax.measure.unit.AlternateUnit.<init>(AlternateUnit.java:50)
at javax.measure.unit.Unit.alternate(Unit.java:299)



There are no comments yet on this issue.