import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("Java");
stringList.add("Example");
stringList.add("Text");
stringList.add(null);
stringList.forEach(str -> System.out.println(str.substring(1))); //error
}
}
In the above example as you can see I am iterating through a ArrayList of Strings that have a null value as well, so when I use forEach loop over it and iterate and do a subString on the array elements, I will get an NPE (Null Pointer Exception)
Output:Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.substring(int)" because "str" is null
at Example.lambda$main$0(Example.java:14)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at Example.main(Example.java:14)
So as a developer you would need to handle the Unchecked NPE!
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("Java");
stringList.add("Example");
stringList.add("Text");
stringList.add(null);
stringList.forEach(str -> {
if(str == null) {
System.out.println("NULL Value");
} else {
System.out.println(str.substring(1));
}
});
}
}

- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
- Fix mySQL Error Cant connect to local MySQL server through socket /var/run/mysqld/mysqld.sock ERROR 2002 HY000 - MySQL
- How to Install Git on Ubuntu Linux - Git
- How to reset eclipse layout - Android
- How to know my IP on Mac Ventura 13.0 - MacOS
- Java: Collect Stream as ArrayList or LinkedList - Java
- Increase Android Emulator Timeout time - Android
- How to install micro text editor using brew on macOS - MacOS
- Install RabbitMQ on Docker - Docker