1

I have problem with my program. I would like to have access to keystore, user give a pass and path. my code:

public class cipher_player {

    public static void main(String[] args) throws Exception{    

        KeyStore klucz = KeyStore.getInstance("JCEKS");

        Console konsola = System.console();
        char passwordArray[] = konsola.readPassword("Password: ");

        java.io.FileInputStream plik_keystore = null;

        try{
            Scanner scanner = new Scanner(System.in);
            System.out.print("Path to keystore: ");
            String pathArray = konsola.readLine();
            System.out.print(pathArray);
            plik_keystore = new java.io.FileInputStream(pathArray);
            klucz.load(plik_keystore, passwordArray);
            if(plik_keystore != null){
                plik_keystore.close();
            }
            scanner.close();

        }

        catch(FileNotFoundException e)
        {
            System.out.println("Keystore not found");
            System.exit(0);
        }

    }
}

and when I run it display this error

Exception in thread "main" java.lang.NullPointerException
    at crypto3.cipher_player.main(cipher_player.java:47)

its line with char passwordArray[] = konsola.readPassword("Password: ");

I don't have idea how solve this problem.

Sully
  • 14,672
  • 5
  • 54
  • 79
  • 3
    konsola is null -- look back into your program to see why. Your system may not allow a console. Not every system does. – Hovercraft Full Of Eels Mar 23 '14 at 12:52
  • 2
    Are you running this code via IDE (Eclipse, NetBeans, IntelliJ)? If so then even if your system does support Console then if your code is run via `javaw.exe` (option without console, default in IDEs) you will also get null as result of `System.console()`. – Pshemo Mar 23 '14 at 12:56
  • There's only one thing in that line that could be null and causing your problem -- `konsola`. – Hot Licks Mar 23 '14 at 13:17

2 Answers2

1

Your System object won't return a valid Console object. Not all allow for this.

As per the System API:

Returns the unique Console object associated with the current Java virtual machine, if any.
Returns:
The system console, if any, otherwise null. <emphasis mine>

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

You're probably trying to run the application from IDE - that's why System.console() returns null. Try running the app from the command line.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
apangin
  • 92,924
  • 10
  • 193
  • 247