Java. Lang. NullPointerException is the most easy to occur in the Java exception, is also very puzzled programming beginners. This paper explains the cause of null pointer exception and gives a quick method to solve the null pointer exception.

What is a null pointer exception

NullPointerExceptions are exceptions that occur when you try to use a reference that does not point to any object. Calling a method on a null reference or a field that attempts to access a null reference triggers a NullPointerException. For example:

public class Dm2box { public static void main(String[] args) { Object obj; obj.hashCode(); }}

The above code will throw a null pointer exception when obj.hashcode () is executed because the obj variable does not yet point to any instance object.

The second line declares a variable obj of type Object, but it doesn’t actually contain the original value, just a pointer, and since you haven’t said what to point to yet, Java sets it to null, which means “I’m not pointing to anything.” The execution of the hashCode() method on the third line causes no method to execute and a null pointer exception to be thrown because the variable does not point to an object in real memory.

This is like asking the students in the first row and the first row to answer the question before the students are seated in a class. At this time, there is no corresponding student in the seat, so there is no one to answer the question. In the end, the behavior of answering the question cannot be carried out because there is no one in the seat. In the example above, the class is the memory space; Students are objects in memory; The seat number is the variable obj; The answer is the hashCode() method; The answer to a question that no counterpart can perform is a null-pointer exception.

How to avoid it when writing

Because NullPointerExceptions inherit from RuntimeExcetion, they do not need to be explicitly described in a method declaration. Such undeclared exceptions cannot be checked by the compiler when the source code is compiled, but can be avoided by writing them as much as possible. Therefore, the following principles should be followed to achieve the goal of avoidance:

  • All variables should point to the corresponding reference object as soon as possible after they are declared, and no operation should be performed on the variable unless it points to an explicitly referenced object.
  • When a public method is defined, all incoming parameters should be checked for non-null if they throw a meaningful exception for null or point to an explicit object. Since private methods cannot be directly referenced by other classes, it is practical to check whether or not they need to be checked.

Diagnosis after an exception occurs

After Java1.4, null pointer exceptions are explicitly indicated as follows:

in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "list" is null

According to the exception prompt, we can quickly locate the specific call stack information that occurs the exception and locate the specific source line, and then we can quickly find the exception point according to the variable name prompted in the exception information. Refer to the previous chapter for processing.

Copyright notice, originally published in
Digital magic box
https://www.dm2box.com/Welcome to reprint.