Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen NullPointerException | Section
Java Fundamentals

bookNullPointerException

NPE

Let's understand the concept of the null value for a string variable. When we create a string variable but forget to initialize, it acquires a null value. In comparison, if we don't initialize an int variable, it acquires a value of 0.

Let's consider an example of a string variable with a null value:

Main.java

Main.java

copy
12345678
package com.example; public class Main { public static void main(String[] args) { String string = null; System.out.println(string); } }

As you can see, the output on the screen is null, and the code seems to be working. But let's now try using any method with a variable with the null value:

Main.java

Main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String string = null; string.toLowerCase(); System.out.println(string); } }

When attempting to call any method on a string variable with a null value, we receive a NullPointerException.

Ways to avoid NPE:

  • Be attentive. If you create a string variable, double-check if you have initialized it;
  • Use a null check before using the variable, for example.
Main.java

Main.java

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { String string = null; if (string == null) { // null check System.out.println("Can't use methods with string, because value is null"); } else { System.out.println(string); } } }
Aufgabe

Swipe to start coding

You're developing a system that processes employee emails.
Some email entries might be missing (null).
Your task is to validate and process only the non-null and properly formatted emails.

  1. You are given an array of employee emails.
  2. Loop through each email in the array.
  3. For each entry:
    • Check if the email is not null
    • And check if it contains the @ symbol
  4. If both conditions are met, print the email to the console.
  5. Otherwise, print:
    "Invalid or missing email"

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 33
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookNullPointerException

Swipe um das Menü anzuzeigen

NPE

Let's understand the concept of the null value for a string variable. When we create a string variable but forget to initialize, it acquires a null value. In comparison, if we don't initialize an int variable, it acquires a value of 0.

Let's consider an example of a string variable with a null value:

Main.java

Main.java

copy
12345678
package com.example; public class Main { public static void main(String[] args) { String string = null; System.out.println(string); } }

As you can see, the output on the screen is null, and the code seems to be working. But let's now try using any method with a variable with the null value:

Main.java

Main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { String string = null; string.toLowerCase(); System.out.println(string); } }

When attempting to call any method on a string variable with a null value, we receive a NullPointerException.

Ways to avoid NPE:

  • Be attentive. If you create a string variable, double-check if you have initialized it;
  • Use a null check before using the variable, for example.
Main.java

Main.java

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { String string = null; if (string == null) { // null check System.out.println("Can't use methods with string, because value is null"); } else { System.out.println(string); } } }
Aufgabe

Swipe to start coding

You're developing a system that processes employee emails.
Some email entries might be missing (null).
Your task is to validate and process only the non-null and properly formatted emails.

  1. You are given an array of employee emails.
  2. Loop through each email in the array.
  3. For each entry:
    • Check if the email is not null
    • And check if it contains the @ symbol
  4. If both conditions are met, print the email to the console.
  5. Otherwise, print:
    "Invalid or missing email"

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 33
single

single

some-alt