import java.io.Console;
public class ConcoleInjava {
public static void main(String[] args) {
Console c=System.console();
System.out.println("enter ur passward :");
char[]ch=c.readPassword();
String pass=String.valueOf(ch);
System.out.println(pass);
}
}

- 17,592
- 13
- 51
- 62

- 17
-
5Where's the stack trace? – MadProgrammer Feb 06 '15 at 21:03
-
1Where is the NullPointerException being raised – challett Feb 06 '15 at 21:03
-
1`Console` appears to be `null`. Have you tried running it from the command line – MadProgrammer Feb 06 '15 at 21:04
-
possible duplicate of [System.console() returns null](http://stackoverflow.com/questions/4203646/system-console-returns-null) – Mike Nakis Feb 06 '15 at 21:05
5 Answers
Console object is not getting created,which is why you are getting NPE.
at the line char[]ch=c.readPassword();
See Here:- The Problem may be related to IDE or no console available
Possible solution:- Refer to this issue on SO
-
-
@MadProgrammer , knew it but,was looking for the best possible solution,so provided the link – dReAmEr Feb 06 '15 at 21:10
My best guess is that the NullPointerException is raised at the char[]ch=c.readPassword();
because the Console c
object has no value in it.
Which means, you are asking for the password value of an object which equals to null.
If you are running your program inside Eclipse you can't get a Console instance.
It is a known eclipse bug.

- 2,343
- 1
- 27
- 38
As you are not initializing the character array it is giving NullPointerException
import java.io.Console;
public class ConcoleInjava {
public static void main(String[] args) {
Console c=System.console();
System.out.println("enter ur passward :");
char[]ch = new char[int];
ch = c.readPassword();
String pass=String.valueOf(ch);
System.out.println(pass);
}
}
In the code in place of "[int]" (line no 6 ) give the size of that array means you are initializing the array. so you wont get the null pointer exception.

- 745
- 2
- 8
- 29
-
That won't work as your are assigning the char array object to the value of a possible return method after it was instantiated this entails the char array object will lose its attributes in the next line of code after it was instantiated. – rert588 Feb 06 '15 at 21:20
-
The method call c.readPassword();
returns null
if the end of stream is
reached
. End of stream means there are no more characters to read in a sense. You may have to do more research on this to gain full clarity from java oracle docs.

- 737
- 1
- 7
- 19