Back to Blog

POV: The Coffee Machine is Broken ☕❌ (NullPointerException IRL)

Sandy LaneSandy Lane

Video: POV: The Coffee Machine is Broken ☕❌ (NullPointerException IRL) by Taught by Celeste AI - AI Coding Coach

Watch full page →

POV: The Coffee Machine is Broken ☕❌ (NullPointerException IRL)

Imagine reaching for your morning coffee, only to find the machine showing an ERROR instead of your cup. This real-life frustration mirrors a common programming error called a NullPointerException, which happens when code tries to use an object that hasn’t been initialized or is missing. Understanding this analogy helps clarify why null values cause crashes in software.

Code

public class CoffeeMachine {
  public static void main(String[] args) {
    String coffee = null;  // The coffee variable is not initialized (null)
    
    // Trying to call a method on a null object causes a NullPointerException
    try {
      System.out.println(coffee.length());  // This will throw NullPointerException
    } catch (NullPointerException e) {
      System.out.println("ERROR: No coffee available! (NullPointerException caught)");
    }
  }
}

Key Points

  • A NullPointerException occurs when code attempts to use an object reference that is null (uninitialized).
  • Just like expecting coffee but getting nothing, null means the absence of a value where one is required.
  • Always check for null before using an object to avoid runtime errors.
  • Handling null values gracefully improves program stability and user experience.