NullPointerException is the abbreviation of java.lang.NullPointerException. It is an exception class in the Java language and is located in the java.lang package. The parent class is java.lang.RuntimeException. This exception does not need to be captured and handled in the source program.
#This exception is thrown when an application attempts to use null where an object is expected. (Recommended learning: Java video tutorial)
Call the instance method of the null object:
class Point { public int x, y; public int getX() { return x; } } public class TestNullPointerException { static Point p1; public static void main(String args[]){ p1.getX(); // 此处抛出NullPointerException } }
The application will throw NullPointerException Instance of the class indicating other illegal uses of null objects.
Java Null pointer errors happen to almost everyone. Java officials are also aware of this problem, so they introduced the OPtional class in Java 8 specifically to solve the security problem of null.
Therefore, we must be very careful and actively check for null when the object may be empty. Otherwise, NullPointerException will be waiting for us there.
For more Java-related technical articles, please visit the Java Development Tutorial column to learn!
The above is the detailed content of NullPointerException. For more information, please follow other related articles on the PHP Chinese website!