官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 遂宁市| 玉田县| 香格里拉县| 肥东县| 普格县| 虞城县| 开封市| 肇源县| 台南市| 绥德县| 瓮安县| 额尔古纳市| 庆安县| 梨树县| 游戏| 成都市| 东平县| 张掖市| 遂川县| 门头沟区| 淄博市| 南皮县| 西昌市| 卓资县| 南丹县| 合肥市| 华蓥市| 阿拉尔市| 庐江县| 台江县| 安远县| 阳西县| 山丹县| 斗六市| 芜湖县| 上虞市| 洪泽县| 南召县| 淮安市| 宜城市| 陆川县|