001    /**
002     * Exercise 9.4 from the book.
003     * 
004     * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk
005     * @version 1.0
006     */
007    public class Exercise9_4 {
008            /**
009             * Throws a NullPointerException
010             */
011            public static void getNullPointerException() {
012                    Object o = null;
013                    String s = o.toString();
014            }
015    
016            /**
017             * Catch a NullPointerException
018             */
019            public static void catchNullPointerException() {
020                    try {
021                            // Set object to null
022                            Object o = null;
023                            // try to call method from java.lang.Object
024                            String s = o.toString();
025                    } catch (NullPointerException e) {
026                            System.out.println("Exception: Object is not initialized");
027                    }
028            }
029    }