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"; &...