// Zorah Fung, CSE 143 // A demo of "null" public class NullDemo { public static void main(String[] args) { Student student = new Student(); System.out.println(student.id); System.out.println(student.name); System.out.println(student.grades); // System.out.println(student.name.toLowerCase()); // throws NullPointerException // System.out.println(student.grades.add(42)); // throws NullPointerException Student student2 = null; /* * All of the following with throw NullPointerException * System.out.println(student2.id); System.out.println(student2.name); * System.out.println(student2.name.toLowerCase()); */ String word = "hello"; String word2 = null; printNotNull("hello"); printNotNull(""); printNotNull("null"); printNotNull(word); printNotNull(word2); // throws IllegalArgument exception printNotNull(null); // throws IllegalArgument exception } // Prints the given string, as long as it is not null. // If the given string is null, an IllegalArgumentException is thrown. public static void printNotNull(String s) { if (s == null) { throw new IllegalArgumentException("String parameter may not be null"); } System.out.println(s); } }