I want to input in java from console, but it is throwing NullPointerException | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to input in java from console, but it is throwing NullPointerException

import java.io.Console; public class ConsoleReadString { public static void main(String[] args) { Console c = System.console(); String s = c.readLine(); System.out.println("You entered string " + s); } } //this throws a nullPointerException //also, I am writing this in eclipse and not codeplayground

12th Jul 2021, 8:07 PM
Yuvraj Singh
Yuvraj Singh - avatar
2 Answers
0
I figured it out, it only runs on console, and not on an ide.
13th Jul 2021, 10:00 AM
Yuvraj Singh
Yuvraj Singh - avatar
+ 1
depends if there is console or not, I ran it on android tablet (by jStudio) and it works. If you don't like Scanner use BufferedReader public static void main(String[] args) throws IOException { //Console c = System.console(); BufferedReader c = new BufferedReader( new InputStreamReader(System.in));
13th Jul 2021, 4:13 AM
zemiak