NullPointerException explained
How many times has a NullPointerException in your code caused the code to break? In this blog post, I will be explaining what a NullPointerException is and what you can do to prevent it. What is a NullPointerException? A NullPointerException is a RuntimeException . It is an unchecked exception and so does not need to be handled by the code. It occurs when you try to manipulate an object that has a null value. When does it occur ? A NullPointerException can occur in several scenarios. Scenario 1 – Invoking a method on an object that has a null value. The most common scenarios where a NullPointerException occurs is when you invoke a method on an object that has a null value. Consider the following code snippet: String str = "Hello World";System.out.println("Length of the String is "+str.length());str = null;System.out.println("Length of the String is "+str.length()); A variable called “ str ” is initialized to ...