Java Reference
In-Depth Information
Yuck! What's going on? Of course the program will fail, because the second element of the list of
points is null. You then try to process a null reference. Because the error occurs in a stream
pipeline, the whole sequence of method calls that make a stream pipeline work is exposed to you.
But notice that the stack trace produces the following cryptic lines:
at Debugging.lambda$main$0(Debugging.java:6)
at Debugging$$Lambda$5/284720968.apply(Unknown Source)
They mean that the error occurred inside a lambda expression. Unfortunately, because lambda
expressions don't have a name, the compiler has to make up a name to refer to them. In this
case it's lambda$main$0, which isn't very intuitive. This can be problematic if you have large
classes containing several lambda expressions.
Even if you use method references, it's still possible that the stack won't show you the name of
the method you used. Changing the previous lambda p -> p.getX() to the method reference
Point::getX will also result in a problematic stack trace:
Note that if a method reference refers to a method declared in the same class as where it's used,
then it will appear in the stack trace. For instance, in the following example
import java.util.*;
public class Debugging{
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3);
numbers.stream().map(Debugging::divideByZero).forEach(System
.out::println);
}
public static int divideByZero(int n){
return n / 0;
}
}
 
Search WWH ::




Custom Search