Cannot invoke "java.io.Console.readLine()" because "c" is null [duplicate] - java

This question already has answers here:
System.console() returns null
(13 answers)
Closed 3 months ago.
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)

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)

Related

I have an error in my Java program, it is visible on Eclipse IDE [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
What is the "String args[]" parameter in the main method?
(18 answers)
Closed 1 year ago.
I have the same error in every code I run on Eclipse IDE or on Notepad, Also it was not earlier. In this code I also having same error which I mentioned below
Code:
package WorkJava;
public class TypeCast{
public static void main(String ar[]) {
int sum=0;
for(int i=0;i<5;i++) {
int a=Integer.parseInt(ar[i]);
sum=sum+a;
}
System.out.println("the sum="+sum);
}
error:-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at WorkJava.TypeCast.main(TypeCast.java:1
you are getting the error because you are not passing program arguments so to pass it in eclipse open your class click run on the menu bar the run configurations then argument then in program arguments type 5 integers like the following with spaces.
2 0 4 5 6

ar.readLine not reading a input from user?

Summary
1. I am import java.io.console,i am not getting any error in my code,
code is correct, I thought, It's getting some error in eclipse
software... Error :
Exception in thread "main" java.lang.NullPointerException at
loops.Escapey.main(Escapey.java:9)
Coding:
package loops;
import java.io.Console;
public class Escapey {
public static void main(String[] args) {
Console ar = System.console(); // creating a new object for console
String name = ar.readLine("how old are you ?"); //reads a user input
System.out.printf("%s - pretty age",name);
}
}
Ouput :
- I except the output how old are you ?? 18 18 - pretty age
But the actual output is getting error... Exception in thread "main"
java.lang.NullPointerException at loops.Escapey.main(Escapey.java:9)
I assume you are running this into some IDE. Since System.console returns the attached console, if you run it into IDE, it will return NULL. The better approach is to use Scanner class. If you really want to use System.console, you will have to test it on some console. If on Mac, run the terminal. On Linux any of the terminal apps would work.
If we run this in IDE (Intellij), it is throwing null pointer exception. Since this is related to console, I tried to execute this in command line and it works fine.
Execute these steps in terminal or command line and it will work
1) javac Escapey.java
2) java Escapey

JAVA Console error [duplicate]

This question already has answers here:
System.console() returns null
(13 answers)
Closed 5 years ago.
If i run this code below in Netbeans
import java.io.Console;
public class Introduction {
public static void main(String[] args) {
Console console= System.console();// creates a java Object which has method that allows us to write
console.printf("Hallo parvel");
}
}
It gives me the error:
Exception in thread "main" java.lang.NullPointerException
at Introduction.main(Introduction.java:10)
/home/parvel/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds).
please help
java.io.Console object is null because there is no console attached to it.
Try to run from command line or attached console to it.
Find more details about it here: https://netbeans.org/bugzilla/show_bug.cgi?id=111337
You need to run this from a terminal using java -jar myjar.jar
In order to have this print to console using System.console();
You will need to export this as a runnable jar
then you can run java -jar myjar.jar and it will output Hallo parvel

How this program compiled and run successfully? [duplicate]

This question already has answers here:
Java Label usage [duplicate]
(2 answers)
Closed 6 years ago.
The below program compiled successfully and run without any errors. As per my understanding is should have thrown error in line 4. Can somebody explain?
class Test{
public static void main(String args[]) {
// my favorite website is
http://www.stackoverflow.com/questions/ask
System.out.println("hello world");
}
}
No, it's not an error, "http:" works here as the name of the label and "//" starts the comments which is ignored.

Why does System.console() return null for a command line app? [duplicate]

This question already has answers here:
System.console() returns null
(13 answers)
Closed 8 years ago.
I am working on a legacy app which depends on user command line input:
String key = System.console().readLine("Please enter the license key: ");
However, I am getting a NullPointerException because System.console() gives me a null.
Why does System.console() return null for a command line app? It happens when running it out of the terminal as well as IDE.
If you start java from a terminal window, then it really should work, even though I haven't tried on OSX.
If you run a simple test using java directly from the terminal, does it work?
echo 'public class Test { public static void main(String[] args) {System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test
Expected output:
hello world
If it doesn't work, then sorry, no console support on your platform.
However, if it works, and your program doesn't then there is a problem with how your program is started.
Check how the java binary started? Is it started from a shell script? Check that stdin/stdout have not been redirected or piped into something, and possibly also that it's not started in the background.
ex: This will probably make System.console() return null.
java Test | tee >app.log
and this:
java Test >/tmp/test.log
This seems to work on my machine (linux)
java Test &
Neither does it seem as if System.setOut, System.setErr or System.setIn affects the console, even after a couple of gc's and finalizers.
However:
Closing (the original) System.out or System.in will disable the console too.
echo 'public class Test { public static void main(String[] args) {System.out.close();System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test
Expected output:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:1)
So; scan your code for places where it closes streams, or passes System.out somewhere it might get closed.
To read from Standard input (command line input) you must use some kind of stream reader to read the System.in stream.
An InputStreamReader initialised by
InputStreamReader(System.in)
lets you read character by character.
However, I suggest wrapping this with a BufferedReader:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String inputLine = reader.readLine();
Must import
java.io.*;

Categories