Great work!

XP to next level

BugEater

Null, Empty, and Whitespace — What's the Difference?

Learning Objectives

By the end of this lesson you will be able to:

  • Explain the technical difference between null, undefined, empty string, and whitespace
  • Identify how each "nothing" type is transmitted in an HTTP form submission
  • Select the input values that specifically target each empty-state variant

Four Ways to Say Nothing

From a user's perspective, an empty form field is an empty form field. But to the backend, there's a critical difference between:

  1. null: The field was not included in the request at all. The server sees the key is absent.
  2. "" (empty string): The field was submitted but with zero characters. The server sees an empty string.
  3. " " (whitespace only): The field was submitted with only space characters. Looks empty, but " ".isEmpty() is false in most languages.
  4. "\t\n" (control characters): Tab, newline, or other invisible whitespace. Passes most length checks; often breaks downstream processing.

Why This Matters in Practice

Consider a username field. The developer writes:

if (username == null || username.isEmpty()) {
    throw new ValidationException("Username is required");
}

This correctly rejects null and empty string. But what happens with " " (three spaces)?

" ".isEmpty() returns false. The username passes validation. The system stores three spaces as a username. Now no one can log in because the login form trims whitespace before comparing.

This is a real class of bugs found in production systems, and it's almost always missed in standard testing.

HTTP Transmission: How "Nothing" Travels

When a form is submitted via HTTP:

What user sees What gets sent Server receives
Empty field username= Empty string ""
No field in form (key absent) null / not present
Spaces only username=+++ " " (URL-decoded)
Tab only username=%09 "\t"

Pro Tip: To test the "field not present" case, you need to bypass the HTML form and send the HTTP request manually without that parameter. A browser form will always send empty string for an empty visible field.

The .strip() / .trim() Fix

A properly written backend will strip whitespace before validation:

if (username == null || username.strip().isEmpty()) {
    throw new ValidationException("Username is required");
}

Your test should verify this fix exists. Submit a username of " " (all spaces). The system should reject it the same way it rejects an empty field.

Key Takeaways

  • Null, empty string, and whitespace-only values are technically different and may be handled differently
  • Whitespace-only inputs often bypass "required" checks that only test isEmpty()
  • HTTP forms transmit empty fields as empty strings, not null
  • To test "field absent", you must send a request without the field — not just leave it empty

Quiz

What is the fundamental difference between null and an empty string?

A user submits a "Name" field containing only spaces. The backend receives the string " ". What risk does this create?

In a JSON API request, a client sends {"username": null} vs {"username": ""}. Why does this distinction matter?

Which test input is MOST likely to reveal a bug in a "required field" validation?