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

  • null means no object; only reference types can hold it.
  • Calling a method on null throws NullPointerException.
  • 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);
  }
}

FAQ: Java Null

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java null examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java null in this Java Java lesson (Java Null).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.