Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
NullPointer Exception | String
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

bookNullPointer Exception

NPE

NullPointerException or NPE is an error every Java programmer is familiar with. This error holds the record for irritating programmers.

First, 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:

java

Main

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:

java

Main

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.

You might think this error is not so serious, but later, when you receive values not written by you personally, you may often come across it. Therefore, it is crucial to understand how to resolve this error now.

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:
java

Main

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); } } }
Task
test

Swipe to show code editor

I have a string array, and I'm trying to print it using a for-each loop, but I keep encountering an error that's already bothering me. Resolve this issue so that the data from my array will be displayed on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 5
toggle bottom row

bookNullPointer Exception

NPE

NullPointerException or NPE is an error every Java programmer is familiar with. This error holds the record for irritating programmers.

First, 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:

java

Main

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:

java

Main

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.

You might think this error is not so serious, but later, when you receive values not written by you personally, you may often come across it. Therefore, it is crucial to understand how to resolve this error now.

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:
java

Main

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); } } }
Task
test

Swipe to show code editor

I have a string array, and I'm trying to print it using a for-each loop, but I keep encountering an error that's already bothering me. Resolve this issue so that the data from my array will be displayed on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 5
toggle bottom row

bookNullPointer Exception

NPE

NullPointerException or NPE is an error every Java programmer is familiar with. This error holds the record for irritating programmers.

First, 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:

java

Main

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:

java

Main

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.

You might think this error is not so serious, but later, when you receive values not written by you personally, you may often come across it. Therefore, it is crucial to understand how to resolve this error now.

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:
java

Main

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); } } }
Task
test

Swipe to show code editor

I have a string array, and I'm trying to print it using a for-each loop, but I keep encountering an error that's already bothering me. Resolve this issue so that the data from my array will be displayed on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

NPE

NullPointerException or NPE is an error every Java programmer is familiar with. This error holds the record for irritating programmers.

First, 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:

java

Main

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:

java

Main

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.

You might think this error is not so serious, but later, when you receive values not written by you personally, you may often come across it. Therefore, it is crucial to understand how to resolve this error now.

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:
java

Main

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); } } }
Task
test

Swipe to show code editor

I have a string array, and I'm trying to print it using a for-each loop, but I keep encountering an error that's already bothering me. Resolve this issue so that the data from my array will be displayed on the screen.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 5. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt