Helpful NullPointerExceptions (NPE) in Java

NullPointerException(NPE) is an exception that the JVM generates if we try to perform some operation on a null value.

Before Java 14, when we get the NPE, it doesn’t say which value is null. Instead, it shows us what line in the code caused the issue.

With Java 14 and the Helpful NullPointerException feature, we can get the error message that describes precisely which variable was null.

Examples of NullPointerException

Let’s write the code that will cause the NullPointerException to explore the program output.

Before Java 14:

class Test {

  public static void main(String[] args) {
    String name = null;
    System.out.println(name.toUpperCase());
  }
}
Output: Exception in thread “main” java.lang.NullPointerException at com.example.Test.main (Test.java:9)
 
Here, we got an error message that the NPE has occurred on line 9. Unfortunately, we can’t conclude which value exactly was null from the message.

Same code in Java 14:

class Test {

  public static void main(String[] args) {
    String name = null;
    System.out.println(name.toUpperCase());
  }
}
Output: Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.toUpperCase()” because “name” is null at com.company.Test.main(User.java:13)
 
As you can see, now we can see what value was null.
 
So this is an enhancement to the traditional NullPointerException, and it’s beneficial when we work with nested objects.
 
Take a look at this example:
class Test {

  public static void main(String[] args) {
    User user = new User("premium", new Credentials()); // we didn't set the values inside the Credentials class
    getUserNameInUpperCase(user);
  }

  private static String getUserNameInUpperCase(User user) {
    return user.getCredentials().getUsername().toUpperCase();
  }
}

class User {
  private String membershipType;
  private Credentials credentials;

  public User(String membershipType, Credentials credentials) {
    this.membershipType = membershipType;
    this.credentials = credentials;
  }

  public String getMembershipType() {
    return membershipType;
  }

  public Credentials getCredentials() {
    return credentials;
  }
}

class Credentials {
  private String username;
  private String password;
  public Credentials() {
  }

  public Credentials(String username, String password) {
    this.username = username;
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public String getPassword() {
    return password;
  }
}
Output: Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.toUpperCase()” because the return value of “com.alegrucoding.Credentials.getUsername()” is null at com.alegrucoding.Test.getUserNameInUpperCase(User.java:13) at com.alegrucoding.Test.main(User.java:8)
 
We got an error message that tells us that the username field from the Credentials class is null.
 
That was all about the Helpful NullPointerException feature in Java!  
 
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *