Java Tutorial
Java Null
null means no object behind a reference. Calling a method on it throws NullPointerException — so check before you use it.
The empty reference
A class-type variable can hold null: a marker that says there is no object yet. That is useful for "optional" values, but it is also how you get the most famous Java crash if you forget to check.
Example
public class Main {
public static void main(String[] args) {
String name = null;
System.out.println(name == null ? "no name yet" : name);
}
}Primitives such as int and boolean cannot be null. Only reference types (String, arrays, your classes) can.
NullPointerException
The moment you call a method or read a field through a null reference, the JVM throwsNullPointerException (often shortened to NPE). The program stops at that line unless you catch it.
Example — crashes at runtime
public class Main {
public static void main(String[] args) {
String text = null;
System.out.println(text.length()); // NullPointerException
}
}Check before you call
The usual guard is an explicit null test. Give the variable a real object, or skip the call when the reference is empty. Larger codebases may use Optional; these lessons keep the check visible.
Example
public class Main {
public static void main(String[] args) {
String text = null;
if (text != null) {
System.out.println(text.length());
} else {
System.out.println("nothing to measure");
}
}
}Assign a real object
Often the fix is simply to initialize before use. Once the reference points at an object, method calls are safe (for that reason — other bugs still exist).
Example
public class Main {
public static void main(String[] args) {
String text = "hello";
System.out.println(text.length()); // 5
}
}Try It Yourself
Exercise: Declare String msg = null;. Print "empty" if it is null, otherwise print the string in uppercase. Then assign a real value and run the same logic again.
Show solution
public class Main {
public static void main(String[] args) {
String msg = null;
if (msg == null) System.out.println("empty");
else System.out.println(msg.toUpperCase());
msg = "hi";
if (msg == null) System.out.println("empty");
else System.out.println(msg.toUpperCase()); // HI
}
}The null check prevents toUpperCase from throwing; after assignment the same branch prints the value.
Key Takeaways
nullmeans no object; only reference types can hold it.- Calling a method on
nullthrowsNullPointerException. - Guard with
if (ref != null)or initialize before use. - Primitives never hold
null— that is a reference-type concept.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Biology
Missing sample id
null means no object yet. A vial without a barcode should be checked before you call length on the string.
Example
public class Main {
public static void main(String[] args) {
String sampleId = null;
System.out.println(sampleId == null ? "unlabelled" : sampleId);
}
}