Java 5 Programs to Generate Null Pointer Exception.five Java programs that generate a `NullPointerException`, along with step-by-step explanations for each.
Program ‘1’ : : Accessing a Null Object’s Method
public class NullPointerExample1 { public static void main(String[] args) { String str = null; // Step 1: Declare a String variable and initialize it to null. System.out.println(str.length()); // Step 2: Attempt to call the length() method on the null object. } }
Explanation:
– Step 1: The variable `str` is declared and set to `null`, meaning it does not point to any object.
– Step 2: When `str.length()` is called, Java tries to access the method of a non-existent object, resulting in a `NullPointerException`.
Program ‘2’ :: Accessing a Null Array Element
java public class NullPointerExample2 { public static void main(String[] args) { String[] array = null; // Step 1: Declare an array variable and initialize it to null. System.out.println(array[0]); // Step 2: Attempt to access the first element of the null array. } }
Explanation:
– Step 1: The variable `array` is declared as a null reference.
– Step 2: Trying to access `array[0]` results in a `NullPointerException` because the array itself does not exist.
Program ‘3’ :: Modifying a Null Object’s Field
java public class NullPointerExample3 { public static void main(String[] args) { Person person = null; // Step 1: Declare a Person object and initialize it to null. System.out.println(person.name); // Step 2: Attempt to access the 'name' field of the null object. } static class Person { String name = "John Doe"; // Step 3: Define a Person class with a 'name' field. } }
Explanation:
– Step 1: The `person` variable is initialized to `null`.
– Step 2: Accessing `person.name` triggers a `NullPointerException` since `person` does not point to an actual `Person` object.
– Step 3: The `Person` class itself is defined, but it is irrelevant since `person` is null.
Program ‘4’ :: Calling a Method on a Null Object\
java public class NullPointerExample4 { public static void main(String[] args) { Object obj = null; // Step 1: Declare an Object variable and initialize it to null. obj.toString(); // Step 2: Attempt to call the toString() method on the null object. } }
Explanation:
– Step 1: The variable `obj` is declared as null.
– Step 2: Calling `obj.toString()` causes a `NullPointerException` because there is no object to invoke the method on.
Program ‘5’ :: Using a Null Value in a Collection
java import java.util.ArrayList; import java.util.List; public class NullPointerExample5 { public static void main(String[] args) { List<String> list = new ArrayList<>(); // Step 1: Create an ArrayList of Strings. list.add(null); // Step 2: Add a null value to the list. System.out.println(list.get(0).length()); // Step 3: Attempt to call length() on the null element. } }
Explanation:
– Step 1: An `ArrayList` of strings is created.
– Step 2: A `null` value is added to the list without issue.
– Step 3: Accessing `list.get(0)` returns `null`, and calling `.length()` on it throws a `NullPointerException`.