Better Strategies for Null Handling in Java

100 %
0 %
Information about Better Strategies for Null Handling in Java

Published on February 3, 2009

Author: Stephan.Schmidt



Most developers handle null not defensive enough. The presentation is about strategies for better null handling in Java to make APIs clearer and prevent Null Pointer Exceptions.

Better Strategies for Null Handling in Java Stephan Schmidt Team manager PMI-3 Berlin, 05.08.2008

Most problematic errors in Java 2 runtime problems in Java ClassCastException „Solved“ with Generics NullPointerException (NPE) Solution? 2

Problems with NPEs RunTime Exception – Point of NPE easy to find – => But not clear where the NULL value comes from 3

Handling of NULL Values Check after Check before String name = map.get(quot;Helloquot;); if (map.containsKey(quot;Helloquot;)) { if (name != null) { String name = map.get(“hallo”); ... } else { … } } else { … } Easy to forget – No support from type system – No tracking of NULL values – Can a reference be NULL ? • 4

Null Handling in Groovy def user = users[“hello”] def streetname = user?.address?.street Safe Navigation Operator ?. user, address can be NULL will simply return NULL instead of throwing an exception 5

Null types in Nice language Nice language NULL types - ?String name => possibly NULL - String name => not NULL String name = null; => Compiler error 6

NULL Handling with Annotations @NotNull, @Nullable in Java IDEA and others, JSR 308 Automatic checks for NULL IDEA tells you when NPEs will occure @NotNull public String get(@NotNull String name) { … } Everything not null and @Optional for NULL better solution 7

Scala Option Class Option can have a value or not (think container with 0 or 1 elements). Subclasses are Some and None Must deal with None (NULL) value, cannot ignore Called Maybe (Just, Nothing) in Haskell map.get(quot;Helloquot;) match { case Some(name) => // do something with name case None => // do nothing } 8

Option in Java Option<String> option = map.get(„hello“); if (option instanceof Some) { String name = ((Some) option).value(); …. } else { // option is none, there is no „hello“ } Explicit handling of „NULL“ value necessary Or: option.isSome() and option.value() without cast 9

For Trick for Option with Iterable Sometimes the none case needs no handling For and Iterable<T> can be used For automatically unwraps Option, does nothing in None case None returns EMPTY list, Some one element list with option value public class Option<T> implements Iterable<T> { … } for (String name: getName(“hello”)) { // do something with name } 10

Convenience methods Option<String> name = none(); Option<String> name = option(dontKnow); Option<String> name = some(„stephan“); 11

How does this method handle NULL values? API makes the intention clear public Option<String> getName() {…} public String getName() { …} public void setName(Option<String> name) { … } public void setName(String name) { … } 12

Easy default values with orElse() String name = map.get(„hello“).orElse(„stephan“); Easy handling of default values Very little code compared to Check Before or Check After for default handling in Java 13


Add a comment

Related pages

Better Null Handling Strategies for Java - @codemonkeyism

Uploaded a presentation on “Better Null Handling Strategies for Java” to SlideShare. Enjoy. View more presentations from Stephan Schmidt. (tags: java null)
Read more

java - Avoiding != null statements - Stack Overflow

Avoiding != null statements. up vote ... a proposal on the table for Java 7 to add "null-safe handling"—a ... Java Null-safe API fits better here to ...
Read more

Exception Handling Strategies - Software Development ...

This tutorial series describes common exception handling strategies in applications. ... You may have noticed my trail on Java Exception Handling, ...
Read more

Null Object pattern - Wikipedia, the free encyclopedia

It can be regarded as a special case of the State pattern and the Strategy ... Java language. As with C++, a null ... null objects. The null object pattern ...
Read more

Ideal Way Of Exception Handling In Multi Layer Java Web ...

Description about better ways of exception handling at ... handling strategies and then ... Handling In Multi Layer Java Web ...
Read more

Java Exception Handling Tutorial with Examples and Best ...

Note that Java Exception handling is a ... always better to define exception handling strategy at the ... exception handling in java, ...
Read more

Strategy pattern - Wikipedia, the free encyclopedia

... the strategy pattern ... This allows better decoupling between the behavior and the class that uses the behavior. ... Strategy Pattern for Java article;
Read more

Java Exception Handling - Software Development ...

... tips and tricks of Java exception handling ... Java exception handling enables your Java ... Handling Strategies. Java Exception Handling ...
Read more

Better Resource Management with Java SE 7

Better Resource Management with Java ... { DataOutputStream out = null; ... this represents a fair amount of code just for properly handling two ...
Read more