0

It's giving null pointer exception. But the same code is generating the output when I run the program from command line.

Console c = System.console();
String s = c.read();
System.out.print(s);

enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Kapil
  • 1
  • Maybe start with the [JavaDocs](https://docs.oracle.com/javase/8/docs/api/java/io/Console.html) - *"Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked"* and *"If no console device is available then an invocation of that method will return null."* – MadProgrammer Jan 11 '18 at 05:38

2 Answers2

0

The method System.console() can return null, if no console device is present. System.console() null exception should be handled if your development required it, in case password reading etc. Other alternate are available if you want to read input from command line.

Using Scanner class Or

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();

System.console development

Reference: illegalargumentexception blog

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Yogesh
  • 91
  • 5
-1

System.console() work perfectly in cmdline

Kutler01
  • 19
  • 1