0

I am trying to get the input from user by using System.console. However, I get a NullPointerException when I run the following code (I am trying to get input from the user).

import java.io.*;
public class Systemlearn {
    public static void main(String[] args)throws IOException {
        Console c=System.console();
        System.out.println("Enter the Name:");
        String str=c.readLine();
        System.out.println(str);
    }

}

Output:

Enter the Name:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.Console.readLine()" because "c" is null
    at week5.Systemlearn.main(Systemlearn.java:7)
Vini
  • 8,299
  • 11
  • 37
  • 49

1 Answers1

0

Are you running this from an IDE? If yes then that is the problem since System.console() is not available in an IDE environment.

Try running it from the console (Command Prompt / Terminal) and it should work fine.

Edit: Just tried in my console and it worked (see screenshot below)

Screenshot

Vini
  • 8,299
  • 11
  • 37
  • 49