how to handle null pointer exception in java 8
While forEach is suitable for a simple action, even the short detour via Optional makes it complex enough to benefit from the Stream API: So Optional is a good return type for a single element which might be absent, e.g. 1. However, this can lead to null pointer exceptions if an attempt is made to dereference a null reference. . As we already mentioned that Optional class is available in the java.util package, hence you will have to import that package before you use it. Let us see an example: Opinions expressed by DZone contributors are their own. Found inside – Page 92