-1

I am working on a login for my USB. I know there are other languages that do that better but I am learning Java. I get an error using the IO Console

package access;
import java.awt.Desktop;
import java.io.*;


public class access {

    public static void main(String[] args)throws IOException {
        BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
        
        Console c = System.console();
         
        Desktop desktop = Desktop.getDesktop();
        File dirToOpen = null;
        
        
        String user = c.readLine("Username: ");
        
        char pass[] = c.readPassword("Enter password: ");
        String uPass = new String(pass);

             
        
        if(user.equals("yuto") && uPass.equals("abascalesgay")) {
            
            try {
                dirToOpen = new File("E:\\encrypted");
                desktop.open(dirToOpen);
            }
            
            catch (IllegalArgumentException iae) {
                System.out.println("File Not Found");
            }
                
            }
        else {
            System.out.println("Credenciales no válidas we, vuelve a intentarlo.");
        }
    }

}

Error log

Exception in thread "main" java.lang.NullPointerException at access.access.main(access.java:17)

Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
aizeenX
  • 21
  • 6
  • 1
    Plz tell what is line 17 which the error comes from – YourHelper Nov 13 '20 at 15:37
  • 1
    According to the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#console--), you will see that method `console()` returns: _The system console, if any, otherwise null._ – Abra Nov 13 '20 at 16:16
  • String user and know I get the same error on line 22 --> char pass[] – aizeenX Nov 13 '20 at 16:21

1 Answers1

0

If you will add this code to your program:

if(c == null)
        {
            System.out.print("No console available");
            return;
        }

you will check if your line Console c = System.console(); returns NULL or not.

Currently it is returning NULL as no console was found and the rest of the code can't compile because of it.

If you are running your program through some IDE - it will not work as IDE is not a console!

  1. Go to "cmd.exe"
  2. type "cd" - hit enter..
  3. now type "java " - hit enter

Another alternative to creating your own BufferedReader object from System.in is to use java.util.Scanner like this:

import java.util.Scanner;

Scanner in;
in = new Scanner(System.in);

String s = in.nextLine();

Also if you want to use the System.console() you shouldn't get password with char pass[] but do it through:

    String username = console.readLine("Username: ");
    String password = new String(console.readPassword("Password: "));
FilipA
  • 526
  • 3
  • 10
  • I mean I get what you mean but the thing is when I enter the passwd I don't want the character to be visible that's why I use the IO Console but now I get the same error on the char pass[] – aizeenX Nov 13 '20 at 16:20
  • Are you starting your program through console or in IDE? – FilipA Nov 13 '20 at 16:28
  • @khandaniyal I will edit my post in a second as it shouldn't be char pass[] either – FilipA Nov 13 '20 at 16:29
  • Never mind dude thx!!!! I was using the Git bash console to run the -jar, I tried with the windows cmd and its running. – aizeenX Nov 13 '20 at 16:48
  • @khandaniyal no worries. Mark this question as answered then :) – FilipA Nov 13 '20 at 16:49