Most of the Java developers encounter NullPointerExceptions while working with Java applications. Java14 enhances the usability of NullPointerException generated by the JVM by describing precisely which variable was null.
let’s see it with an example.
NullPointerDemo.java
public class NullPointerDemo {
public static void main(String args[]) {
String str = null;
System.out.println("Length of str is "+ str.length());
}
}
When you run above program on jdk prior to 14, you will get following output.
Exception in thread "main" java.lang.NullPointerException at NullPointerDemo.main(NullPointerDemo.java:5)
With the above output, you don’t know what is happened exactly, you need to go to the file NullPointerDemo.java and check the line number 5.
But when you ran same application in Java14, you will get following error message.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null at NullPointerDemo.main(NullPointerDemo.java:5)
As you see, from the above message, we can clearly understand that null pointer exception occurred on a variable of type string while calling the length() method.
Let’s see one more complex example.
Address.java
public class Address {
private String city;
private String street;
private String country;
public Address(String city, String street, String country) {
super();
this.city = city;
this.street = street;
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Employee.java
public class Employee {
private Integer id;
private String name;
private Address address;
public Employee(Integer id, String name, Address address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
NullPointerDemo1.java
public class NullPointerDemo1 {
private static void printStrInUpperCase(String str) {
try {
System.out.println(str.toUpperCase());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void printEmployee(Employee emp) {
printStrInUpperCase(emp.getName());
printStrInUpperCase(emp.getAddress().getCity());
printStrInUpperCase(emp.getAddress().getStreet());
printStrInUpperCase(emp.getAddress().getCountry());
}
public static void main(String args[]) {
Address addr1 = new Address("Bangalore", null, "India");
Employee emp1 = new Employee(1, "Krishna", addr1);
Employee emp2 = new Employee(2, null, null);
printEmployee(emp1);
printEmployee(emp2);
}
}
Output prior to Java14
KRISHNA BANGALORE java.lang.NullPointerException at NullPointerDemo1.printStrInUpperCase(NullPointerDemo1.java:5) at NullPointerDemo1.printEmployee(NullPointerDemo1.java:15) at NullPointerDemo1.main(NullPointerDemo1.java:26) INDIA java.lang.NullPointerException at NullPointerDemo1.printStrInUpperCase(NullPointerDemo1.java:5) at NullPointerDemo1.printEmployee(NullPointerDemo1.java:13) at NullPointerDemo1.main(NullPointerDemo1.java:27) Exception in thread "main" java.lang.NullPointerException at NullPointerDemo1.printEmployee(NullPointerDemo1.java:14) at NullPointerDemo1.main(NullPointerDemo1.java:27)
Output in Java14
$java NullPointerDemo1
KRISHNA
BANGALORE
java.lang.NullPointerException: Cannot invoke "String.toUpperCase()" because "<parameter1>" is null
at NullPointerDemo1.printStrInUpperCase(NullPointerDemo1.java:5)
at NullPointerDemo1.printEmployee(NullPointerDemo1.java:15)
at NullPointerDemo1.main(NullPointerDemo1.java:26)
INDIA
java.lang.NullPointerException: Cannot invoke "String.toUpperCase()" because "<parameter1>" is null
at NullPointerDemo1.printStrInUpperCase(NullPointerDemo1.java:5)
at NullPointerDemo1.printEmployee(NullPointerDemo1.java:13)
at NullPointerDemo1.main(NullPointerDemo1.java:27)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Address.getCity()" because the return value of "Employee.getAddress()" is null
at NullPointerDemo1.printEmployee(NullPointerDemo1.java:14)
at NullPointerDemo1.main(NullPointerDemo1.java:27)
Helpful NullpointerExceptions feature offers
a. helpful information to developers and support staff about the premature termination of a program.
b. Improve program understanding by more clearly associating a dynamic exception with static program code.
c. Reduce the confusion and concern that new developers often have about NullPointerExceptions.
Reference
https://openjdk.java.net/jeps/358
No comments:
Post a Comment