- The Java Workshop
- David Cuartielles Andreas G?ransson Eric Foster Johnson
- 698字
- 2021-06-11 13:05:26
NullPointerException – Have No Fear
We presented the concept of null within Java in a previous chapter. As you may recall, null is the value that is implicitly assigned to an object upon creation, that is, unless you assign a different value to it. Related to null is the NullPointerException value. This is a very common event that can and will happen to you for a variety of reasons. In this section, we will highlight some of the most common scenarios of this in an effort to introduce you to a different way of thinking when dealing with any type of exception in your code.
In Example01, we examined the process of trying to perform operations on an object that was pointing to null. Let's look at some other possible cases:
public class Example04 {
public static void main(String[] args) {
String vehicleType = null;
String vehicle = "car";
if (vehicleType.equals(vehicle)) {
System.out.println("it's a car");
} else {
System.out.println("it's not a car");
}
}
}
The outcome of this example would be the following:
Exception in thread "main" java.lang.NullPointerException
at Example04.main(Example04.java:5)
Process finished with exit code 1
You could have prevented this exception if you had written your code to compare the existing variable with the potentially null one instead.
public class Example05 {
public static void main(String[] args) {
String vehicleType = null;
String vehicle = "car";
if (vehicle.equals(vehicleType)) {
System.out.println("it's a car");
} else {
System.out.println("it's not a car");
}
}
}
The preceding code will produce the following result:
it's not a car
Process finished with exit code 0
As you can see, there is no conceptual difference between the examples; however, there is a difference at the code level. This difference is enough for your code to issue an exception upon compilation. This is because the equals() method for the String class is prepared to handle the situation of its parameter being null. On the other hand, a String variable that is initialized to null cannot have access to the equals() method.
A very common situation for provoking a NullPointerException occurs when trying to call non-static methods from an object initialized to null. The following example shows a class with two methods that you can call to see whether they produce the exception. You can do this by simply commenting or uncommenting each of the lines calling the methods from main(). Copy the code in the IDE and try the two cases:
public class Example06 {
private static void staticMethod() {
System.out.println("static method, accessible from null reference");
}
private void nonStaticMethod() {
System.out.print("non-static method, inaccessible from null reference");
}
public static void main(String args[]) {
Example06 object = null;
object.staticMethod();
//object.nonStaticMethod();
}
}
There are other cases when this exception can appear, but let's focus on how to deal with exceptions. The following sections will describe different mechanisms you can use to enable your programs to recover from unexpected situations.
- Raspberry Pi for Secret Agents(Third Edition)
- 前端架構:從入門到微前端
- JavaScript從入門到精通(第3版)
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- Redis Essentials
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- Salesforce Reporting and Dashboards
- Getting Started with Polymer
- Python計算機視覺和自然語言處理
- PHP動態網站開發實踐教程
- Android項目實戰:博學谷
- Java Web應用開發
- C語言從入門到精通(微視頻精編版)
- C#程序設計基礎與實踐
- Java語言程序設計與實現(微課版)