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 - Warn when binary numeric promotion applies to the second or third operand of the ternary operator and the result is boxed
Summary: Warn when binary numeric promotion applies to the second or third operand of ...
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.0
Hardware: PC Windows 7 x64
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-15 01:49 UTC by dtrebbien
Modified: 2013-09-02 14:21 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.