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));
}
});
}
}

Java 8 Exception - cannot invoke because the object is null
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Resolve - zsh: command not found: code - zsh
- How to turn off CR LF CRLF in Notepad++ - NotepadPlusPlus
- Rust: Write and Run Hello World! Program Example - Rust
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed. - Java
- Change Current User Password using Mac Terminal Command - MacOS
- 25: How to rename a file using Python Program - Python-Programs
- Android Emulator] ##KBD: Full queue, lose event Error Logs - Android
- How to list all tables using Java JDBC - Java