-1
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);
    }

}
benscabbia
  • 17,592
  • 13
  • 51
  • 62

5 Answers5

2

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

Community
  • 1
  • 1
dReAmEr
  • 6,986
  • 7
  • 36
  • 63
0

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.

0

If you are running your program inside Eclipse you can't get a Console instance.

It is a known eclipse bug.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429

pablosaraiva
  • 2,343
  • 1
  • 27
  • 38
0

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.

Manoj Kumar
  • 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
  • giving the array size i still facing this problem..... – Md Hasan Feb 06 '15 at 21:27
0

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.

rert588
  • 737
  • 1
  • 7
  • 19