Preventing NullPointerExceptions

In Fig. 7.10, we created a deck array of 52 Card references—each element of every reference-type array created with new is default initialized to null. Reference-type variables which are fields of a class are also initialized to null by default. A NullPointerException occurs when you try to call a method on a null reference. In industrial-strength code, ensuring that references are not null before you use them to call methods prevents NullPointerExceptions.

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.