Open links in new tab
  1. A NullPointerException occurs when you try to use a reference that points to null as if it were an actual object. With Scanner, this often happens if you declare a variable but never assign it a value before using it.

    Example:

    public class Example {
    public static String userInput;

    public static void main(String[] args) {
    // This will throw NullPointerException because userInput is null
    System.out.println(userInput.length());
    }
    }
    Copied!

    In this case, userInput is declared but never initialized, so calling .length() on it causes the exception.

    When working with Scanner, a common mistake is shadowing variables or forgetting to assign the scanned value to the intended field. For example:

    public static String user_input;

    public static void input() {
    Scanner scanner = new Scanner(System.in);
    // Wrong: reading input but not assigning to the class variable
    String temp = scanner.nextLine();
    }
    Copied!

    Later, if you loop over user_input.length(), it will throw an NPE because user_input is still null.

    Corrected version:

    import java.util.Scanner;

    public class Example {
    public static String user_input;

    public static void input() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter text: ");
    user_input = scanner.nextLine(); // Assign directly to the static variable
    }

    public static void main(String[] args) {
    input();
    for (int i = 0; i < user_input.length(); i++) {
    System.out.println(user_input.charAt(i));
    }
    }
    }
    Copied!
    Feedback
  1. java - NullPointerException using Scanner - Stack Overflow

    Dec 15, 2013 · I am new to programming and I'm starting to create a simple calculator in Java, but I keep getting an error on the line for (int i = 0; i < user_input.length (); i++) The error says: Except...

    • Reviews: 3
    • How to Fix NullPointerException in Java: Complete Guide

      Apr 2, 2026 · Fix NullPointerException in Java with proven patterns. Covers Java 14+ enhanced messages, Optional class, @NonNull annotations, and Spring Boot scenarios.

      Missing:
      • Scanner
      Must include:
    • How to Resolve java.lang.NullPointerException in Console ...

      Learn how to fix java.lang.NullPointerException errors when handling console input in Java. Step-by-step solutions and code examples included.

      Missing:
      • Scanner
      Must include:
    • Null Pointer Exception in Java - GeeksforGeeks

      Aug 5, 2025 · To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before we use them. When we declare a reference …

      Missing:
      • Scanner
      Must include:
    • Java NullPointerException Explained 2025 | Towards Dev

      Oct 15, 2025 · Learn the causes and fixes for Java NullPointerException. Step-by-step guide with examples to prevent this runtime error in your Java programs.

      Missing:
      • Scanner
      Must include:
    • The Null Pointer Exception Trap: How to Escape

      Sep 15, 2024 · Discover the hidden dangers of Null Pointer Exceptions (NPEs) in Java development. Learn how to identify, prevent, and debug these common errors to avoid costly mistakes and improve …

    • How Do I Fix NullPointerException in Java Applications?

      Dec 3, 2025 · NullPointerException (NPE) is one of the most common and frustrating errors in Java applications. It occurs when your code tries to access …

      Missing:
      • Scanner
      Must include:
    • NullPointerException Crash Your Java App? Here's How …

      Nov 28, 2025 · NullPointerException is the most frequently thrown exception in Java applications, accounting for countless crashes. It occurs when your code …

      Missing:
      • Scanner
      Must include:
    • Handling NullPointerException in Java: An Expert Guide

      Aug 25, 2024 · Java developers will undoubtedly encounter NullPointerExceptions (NPEs) during projects. This comprehensive 4-part guide dives deep into the root causes, prevention, and mitigation …

      Missing:
      • Scanner
      Must include:
    • How to Fix and Avoid Null Pointer Exception in Java

      Jan 24, 2024 · In this article, we will see how to fix and avoid NullPointerException in Java with examples. The NullPointerException is a runtime exception in Java that occurs when a variable or …

      Missing:
      • Scanner
      Must include:
    • Deep dive into Java NullPointerException Using Scanner

    By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy