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

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.

主站蜘蛛池模板: 建平县| 长丰县| 五指山市| 高密市| 桑植县| 垦利县| 雷波县| 阆中市| 桑日县| 上饶县| 中方县| 通道| 保德县| 高碑店市| 吉首市| 潜江市| 周宁县| 泊头市| 旬阳县| 贵定县| 什邡市| 泊头市| 南溪县| 凤台县| 石棉县| 万宁市| 盱眙县| 乡城县| 宣城市| 乐至县| 布尔津县| 三穗县| 扎鲁特旗| 天水市| 郧西县| 临夏县| 海口市| 北安市| 浦城县| 井陉县| 神池县|