import java.util.Objects; import java.util.Locale; public final class NullPointerDebuggingLab { private NullPointerDebuggingLab() {} static final class Address { private final String city; Address(String city) { this.city = city; } String getCity() { return city; } } static final class Student { private final Address address; Student(Address address) { this.address = address; } Address getAddress() { return address; } } static String cityLabel(Student student) { Objects.requireNonNull(student, "student must not be null"); Address address = student.getAddress(); if (address == null) { return "City unavailable"; } String city = address.getCity(); return city == null || city.trim().isEmpty() ? "City unavailable" : city.toUpperCase(Locale.ROOT); } private static void expect(String expected, String actual) { if (!expected.equals(actual)) { throw new AssertionError( "Expected '" + expected + "' but received '" + actual + "'" ); } } public static void main(String[] args) { expect("JAIPUR", cityLabel(new Student(new Address("Jaipur")))); expect("City unavailable", cityLabel(new Student(null))); expect("City unavailable", cityLabel(new Student(new Address(null)))); expect("City unavailable", cityLabel(new Student(new Address("")))); expect("City unavailable", cityLabel(new Student(new Address(" ")))); try { cityLabel(null); throw new AssertionError("Expected a NullPointerException"); } catch (NullPointerException exception) { expect("student must not be null", exception.getMessage()); } System.out.println("All null-contract checks passed."); } }