BaşlayınÜcretsiz Başlayın

Edge cases: NullPointerException

Of all possible edge cases, the null variable one, where an object has not been set, is the one that tends to sneak up on programmers the most. It can arise for many reasons: someone forgot to initialize a variable, an object did not form properly, someone filled in a website form incorrectly, … The list goes on and on.

In Java, when a method is called on a null variable, a NullPointerException is thrown.

In this exercise, try to trigger a NullPointerException in the given code.

Bu egzersiz

Introduction to Testing in Java

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Call a method on name that will result in a NullPointerException being thrown.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

public class NullPointer {

    private static String name;

    public static void main(String[] args) {
    	// Call a method on name that will throw a NullPointerException
        System.out.println("My name has " + name.____() + " characters.");
    }
}
Kodu Düzenle ve Çalıştır