- 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.
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- Learning Flask Framework
- Getting Started with CreateJS
- Apache Mesos Essentials
- Responsive Web Design by Example
- 精通Python設計模式(第2版)
- Learning Concurrent Programming in Scala
- Windows Embedded CE 6.0程序設計實戰
- Couchbase Essentials
- C語言程序設計與應用(第2版)
- Node.js實戰:分布式系統中的后端服務開發
- Android智能手機APP界面設計實戰教程
- Java EE應用開發及實訓
- Go Programming Cookbook(Second Edition)
- Python樹莓派編程