JDK-5030232 : Add Nice Option types to Java to prevent NullPointerExceptions
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-04-12
  • Updated: 2006-12-11
  • Resolved: 2006-12-11
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Name: jl125535			Date: 04/12/2004


A DESCRIPTION OF THE REQUEST :
Nice is a Java derived language that offers language level support for prevention of NullPointerExceptions.  I propose that this support be added to the Java language.

Type safety in Nice
http://nice.sourceforge.net/safety.html#id2433011

Option types
http://nice.sourceforge.net/manual.html#optionTypes

Since this is a language change, with the potential to break much old code (as well as uncover many old bugs) it should be introduced along with a compiler flag.

JUSTIFICATION :
NullPointerExceptions are extremely common, yet often offer very little information about the real source of the problem.  While they are a runtime problem, adding the requested compiler support would often allow them to be detected and eliminated at compile time.

See:

The Dangling Composite bug pattern
http://www-106.ibm.com/developerworks/library/j-diag2

The Null Flag bug pattern
http://www-106.ibm.com/developerworks/library/j-diag3.html

While it is possible to use aggressive run-time checking to detect bugs earlier at run-time, this has a number of disadvantages.

- It doesn't detect bugs at compile time
- It places the burden on the programmer
- It adds to the clutter of the code
- It assumes that code will handle nulls correctly, unless the code specifies otherwise.  This assumption is usually false.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Variable declarations without the ? prefix assert that they must not be null.  Variable declarations with the ? prefix may be null.  Based on this knowledge, the compiler should produce an error for code that may produce a NullPointerException, when this feature is enabled.
ACTUAL -
There is no current compile support for this.

---------- BEGIN SOURCE ----------
	void foo(String arg) {...}
	
	void bar(?String arg) {
		if (arg != null) {
			//Here Nice knows arg is not null, so it can
			// be passed to a method which takes a String.
			foo(arg);
		}
		foo(arg); //Here arg may or may not be null,
			  //so Nice gives a compilation error.
	}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
There are some obvious work-arounds, but they all fall far short of adding this support to the language.

1) Use Nice instead of Java.  This means decreased tool support, lack of supporting vendors, etc..
2) Use a static bytecode analysis tool to detect NullPointerExceptionS.  While such a tool is possible, I am unaware of one.
3) Aggressively check for nulls in programs.  This clutters code much more than language support would.  Worse yet, it only allows problems to be detected earlier at run time, instead of at compile time.
(Incident Review ID: 249785) 
======================================================================

Comments
EVALUATION In Nice, a type name like String is non-nullable by default, and you have to say ?String for the nullable String type. We could not possibly accept this default in Java. Since 6207924 accepts that types are nullable by default, I am making it the master request for non-nullable types.
11-12-2006

EVALUATION Probably a good idea. JSR305 might define a standard annotation to mean 'non-null'.
20-11-2006