This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 199412

Summary: Warn when binary numeric promotion applies to the second or third operand of the ternary operator and the result is boxed
Product: java Reporter: dtrebbien
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: normal CC: dtrebbien
Priority: P3    
Version: 7.0   
Hardware: PC   
OS: Windows 7 x64   
Issue Type: ENHANCEMENT Exception Reporter:

Description dtrebbien 2011-06-15 01:49:30 UTC
It would be nice if Netbeans could issue a warning when binary numeric promotion applies to the second or third operand of the conditional operator and the resulting value is boxed.  For example, if `integer` is of type Integer, then this code results in a NullPointerException if `integer` is null:

   Integer i = true ? integer : 0;

For another example (`time` is a Long and null):

   time = true ? time : System.currentTimeMillis();

See Bug ID 7029081 NullPointerException from "cond ? : true : false" type operation (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7029081)


Both of these examples can be fixed with the use of the `valueOf` static method:

   Integer i = true ? integer : Integer.valueOf(0);

   time = true ? time : Long.valueOf(System.currentTimeMillis());


The warning should not appear when the condition ensures that the value is not null:

   Integer i = (some-expression && integer != null) ? integer : 0; // This is okay.
   time = time != null ? time : System.currentTimeMillis(); // This is also okay.