title: Masking password input from the console : Java
body: <p>How to mask a password from console input? I'm using Java 6.</p>
  
  <p>I've tried using <code>console.readPassword()</code>, but it wouldn't work. A full example might help me actually.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.Console;
  import java.io.IOException;
  import java.io.InputStreamReader;
  
  public class Test 
  {   
      public static void main(String[] args) 
      {   
          Console console = System.console();
  
          console.printf("Please enter your username: ");
          String username = console.readLine();
          console.printf(username + "\n");
  
          console.printf("Please enter your password: ");
          char[] passwordChars = console.readPassword();
          String passwordString = new String(passwordChars);
  
          console.printf(passwordString + "\n");
      }
  }
  </code></pre>
  
  <p>I'm getting a NullPointerException...</p>