The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Java
What is null, and why does NullPointerException happen?
The variable name does not hold an object such as a string itself. It holds a value that refers to that object (a reference). null is a special value that you store in this place to say "refers to no object," and in code you write it like String name = null;. In name.length(), the dot (.) means "ask the object that name refers to," but if name is null there is no object to ask, so an exception called NullPointerException is thrown. if (name != null) is a check that first asks whether name refers to an object. The empty string "", which has no characters at all, is not null. It is a string object with 0 characters. In this lab you change the value stored in name to "kim", null, or "", and you add or remove the if check. The same code is compiled and run, and you see what is printed and whether an exception is thrown.
Checked against real execution · Compiled 6 combinations with javax.tools on JDK 21, ran them as java processes, and checked the results
public class Main {
public static void main(String[] args) {
// Variable: refers to a string object, or to nothing if it is null
String name = "kim";
System.out.println(name.length());
}
}
The compiler and runtime are JDK 21.0.12.1 (javac 21, java 21). All 6 combinations were checked against real runs: the same source as the code on the screen (identical line by line) was compiled with javax.tools, and the compiled Main was run in a new java process, and the standard output, the first line of the standard error, and the exit code were captured. What was checked is whether compilation succeeded, the output, whether an exception was thrown (whether the first line of the standard error is NullPointerException) and the line where it was thrown (the line the stack trace pointed to is the name.length() line), and the exit code. The "length() runs" column of the table was worked out from the real output by whether a character count appeared in it, and the "if check" column by which block's output appeared. The facts that the step sentences rely on were checked with a separate program. For each of the three values: name == null is false for "kim", true for null, and false for ""; length() is 3, (cannot be called because it is null), and 0; and the line String name = null; runs without an exception, the exception is thrown at the next line, name.length(), and the program ends with exit code 1. I opened the documentation myself. That the null type has only one value, the null reference, and that it can be stored in any reference type follows sections 3.10.8 and 4.1 of the Java Language Specification (Java SE 21); that a reference value either refers to an object or is the null reference that refers to no object follows section 4.3.1; the rules for comparing references with == and != follow section 15.21.3; that an if statement runs the first statement when the condition is true and the else statement when it is false follows sections 14.9.1 and 14.9.2; and that a string literal is, at run time, a value that refers to a String object, and that "" is the empty string, follows section 3.10.5. The NullPointerException API documentation says that it is thrown when an instance method of a null object is called (Java SE 21 API), and I also opened the descriptions of String.length() and isEmpty() in the same API documentation. The sentence that a NullPointerException is thrown when the target of an instance method call is null I confirmed in chapter 15 (section 15.6) of the JLS, but I could not open the text of sections 15.12.4.1 and 15.12.4.4, which specify the call handling in detail. The javac on the PC used to write this lab does not put variable names in the class file, so the detail message of the NullPointerException showed <local1> instead of name. This message is not specified by the specification but was observed on this JDK, so it is not used on the screen. The code on the screen is exactly the shape that really compiles, and nothing was shortened. Beyond that, primitive variables such as int and null, arrays or fields that are null at first, comparing two strings with == and equals, and how to handle a value that may be null with Optional are not covered. Optional is covered in the in-depth lab "Optional".
Previous lab: References · Next lab: Constructors
Struck-through text was removed from the first version of the code; highlighted text was added.