- 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.
- 多媒體CAI課件設計與制作導論(第二版)
- Python程序設計教程(第2版)
- LabVIEW入門與實戰開發100例
- MariaDB High Performance
- INSTANT CakePHP Starter
- 你必須知道的204個Visual C++開發問題
- 老“碼”識途
- Getting Started with SQL Server 2012 Cube Development
- 零基礎學Java程序設計
- 手把手教你學C語言
- 名師講壇:Spring實戰開發(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- Java圖像處理:基于OpenCV與JVM
- 現代C:概念剖析和編程實踐
- DB2SQL性能調優秘笈
- Java高手是怎樣煉成的:原理、方法與實踐