๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ avoiding nullpointerexception in java 8
Avoiding NullPointerException in Java 8
August 26, 2019 - In this post, we explore some simple strategies to avoid the NullPointerException. Let's get started. Different languages provide different methods for checking. Unfortunately, Java is not among them. So, we have to check our variables and objects beforehand. Back in Java 7, there was a proposal to add a simplified method that checked for this exception.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ null-pointer-exception-in-java
Null Pointer Exception in Java - GeeksforGeeks
April 24, 2026 - We can use the ternary operator for handling null pointers. ... import java.io.*; class Geeks { public static void main(String[] args) { String s = null; String m = (s == null) ? "" : s.substring(0, 5); System.out.println(m); s = "Geeksforgeeks"; ...
๐ŸŒ
amitph
amitph.com โ€บ home โ€บ java โ€บ avoid nullpointerexception using java optional
Avoid NullPointerException using Java 8 Optional | amitph
November 22, 2024 - A detailed guide with examples to null values and NullPointerExceptions, and using Java Optional to avoid NullPointerException and null checks
๐ŸŒ
DEV Community
dev.to โ€บ arpitmandliya โ€บ java-8-optional-a-way-to-avoid-nullpointerexception-10g1
Java 8 Optional: A way to avoid NullPointerException - DEV Community
April 11, 2019 - This occurs more often when you call library function and it returns null and you forget to check it. You can use Optional to solve this problem. You can change "JavaOptionalMain" as below.
๐ŸŒ
Medium
medium.com โ€บ @gurkanucar โ€บ how-to-prevent-null-pointer-exceptions-in-java-8d7c894c9bb4
How to Prevent Null Pointer Exceptions in Java? | by Gรผrkan Uร‡AR | Medium
June 8, 2024 - Using optional, we can chaining properties with map and safely retrieve what we need without worrying about NullPointerException. String address = Optional.ofNullable(customer) .map(Customer::getMainAddress) .map(Address::getPostalAddress) ...
๐ŸŒ
Oracle
oracle.com โ€บ java โ€บ technical details
Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!
In addition, Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions. Chapter 9, "Optional: a better alternative to null," from Java 8 in Action: Lambdas, Streams, and Functional-style Programming
๐ŸŒ
Pluralsight
pluralsight.com โ€บ blog โ€บ software development
How to handle (and avoid) NullPointerExceptions in Java | Pluralsight
The practice of unit testing is also generally important and can come to your aid when preventing null pointers. For example, consider a test for Widget, our class from earlier, that ensures it is robust against null usage:
Find elsewhere
๐ŸŒ
DZone
dzone.com โ€บ data engineering โ€บ databases โ€บ avoiding nullpointerexception
Avoiding NullPointerException
December 17, 2020 - public Customer(@NonNull Date birthDate) { // 3 if (birthDate == null) { // 1 throw new IllegalArgumentException(); } this.birthDate = Objects.requireNonNull(birthDate); // 2 } The code above contains 3 alternative ways to do the same thing, any single one is of course enough: ... One-liner check using Java 8 java.util.Objects - most widely used in projects under development today
๐ŸŒ
Iamprafful
blog.iamprafful.com โ€บ 6-tips-and-tricks-to-avoid-null-pointer-exception
6 Tips and Tricks to avoid NullPointerException | Prafful Lachhwani
February 11, 2022 - One of the famous ways to avoid java.lang.NullPointerException is to add null checks wherever plausible. However, this approach will make your code bloated with null != something statements.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-avoid-nullpointerexception-in-java-using-optional-class
How to avoid NullPointerException in Java using Optional class? - GeeksforGeeks
August 6, 2025 - String value is not present Note: Hence this can be understood as an exception handling method for NullPointerException NullPointerException Handling using Optional class: ... // Java program to handle NullPointerException // using Optional Class import java.util.Optional; public class Example { public static void main(String[] args) { // Create a String of size 10 String[] a = new String[10]; // Define the a[1] element a[1] = "geeksforgeeks"; // Create an Optional Class instance // and get the state for a[1] element // for Null value Optional<String> check = Optional.ofNullable(a[1]); // If t
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2013 โ€บ 05 โ€บ ava-tips-and-best-practices-to-avoid-nullpointerexception-program-application.html
Java Tips and Best practices to avoid NullPointerException in Java Applications
null!=Object,; Object!=null ; which one throws null pointer exception ... Anonymous said... Java 8 has added a class called Optional which will make null handling and avoiding null pointer exception more easy.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ exception handling โ€บ java nullpointerexception
Handling Java NullPointerException and Best Practices
October 1, 2022 - Even if the object is null in this case, it will not give an exception and will print โ€˜nullโ€˜ to the output stream. An awesome tip to avoid NPE is to return empty strings or empty collections rather than null. Java 8 Optionals are a great ...
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ how to catch and fix nullpointerexception in java
How to Catch and Fix NullPointerException in Java
November 29, 2025 - Exception in thread "main" java.lang.NullPointerException at NullPointerExceptionExample.printLength(NullPointerExceptionExample.java:3) at NullPointerExceptionExample.main(NullPointerExceptionExample.java:8) The NullPointerException can be avoided using checks and preventive techniques like the following:
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-avoid-null-pointer-exception-in-java
How to avoid null pointer exception in Java - Javatpoint
How to avoid null pointer exception in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
N47
north-47.com โ€บ home โ€บ null pointer exceptions in java 8
Null pointer exceptions in Java 8 โ€” N47
April 10, 2019 - One possible solution for handling null pointer exceptions when the object is not present would be throwing a custom exception for the specific object.
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ null-pointer-exception-java
Understanding and Handling Null Pointer Exceptions in Java โ€” javaspring.net
Use Optional Class: Java 8 introduced the Optional class, which is a container object that may or may not contain a non-null value. You can use Optional to avoid NullPointerException.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ what is nullpointerexception in java & how to avoid it
What Is NullPointerException In Java & How To Avoid It
April 1, 2025 - First, we must ensure that the objects that we use in our programs are initialized properly so that we can avoid the use of null objects that result in a Null Pointer Exception. We should also take care that the reference variables used in the ...
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ null pointer exception in java
Null Pointer Exception in Java - Intellipaat
February 3, 2026 - We can avoid this exception by following best practices such as checking for null before accessing objects and using Objects.requireNonNull(), comparing strings with literals first, and using the ternary operator to assign default values.