1

I wanted to know why does my Java program work in console when I do :

javac Main.java

java Main

...and not in Eclipse, as I have this error :

Exception in thread "main" java.lang.NullPointerException at codePin.main.main(main.java:48) --> char passwordArray[] = console.readPassword("Enter pin: ");

Here's my code :

package codePin;

import java.io.*;
import java.util.*;

public class main {


    static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
        boolean err = false;
        try {
            Scanner scanner = new Scanner(dataFile);
            String line;
            while (scanner.hasNext()) {
                line = scanner.nextLine();
                try {
                    data.add(Integer.parseInt(line));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    err = true;
                }
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            err = true;
        }

        return err;
    }

    public static void main(String[] args) {


        System.out.println("-----------------------");
        System.out.println("APPLICATIONS BESOINS");
        System.out.println("-----------------------");
        Console console = System.console();

        System.out.println(console == null);

        int pinSize = 0;
        int nbTry = 0;
        boolean authenticated = false;


        do {
            do {

                char passwordArray[] = console.readPassword("Enter pin: "); //This is the line causing the error 
                pinSize = passwordArray.length;

                if (pinSize != 4) { 

                    System.out.println("Pin must be 4 digits");
                } else {
                    System.out.println("Checking...");
                }

                ArrayList<Integer> pins = new ArrayList<Integer>(); 
                readPinsData(new File("bdd.txt"), pins); 



                String[] thePins = new String[pins.size()];
                for (int i = 0; i < thePins.length; i++) {
                    thePins[i] = pins.get(i).toString();
                }

                String passEntered = String.valueOf(passwordArray);

                for (int i = 0; i < thePins.length; i++) {                      
                    if (passEntered.equals(thePins[i]) && pinSize == 4) {
                        System.out.println(":)");
                        authenticated = true;
                        break;
                    }
                }

            } while (pinSize != 4); 
            if (!authenticated && pinSize == 4) { 

                System.out.println(":(");
                nbTry++;
            }
        } while (nbTry < 3 && !authenticated);
    }
}

As you can see, I added System.out.println(console == null); at the beggining of my public static void main method just to check, and it does actually return true in the console.

So my question is : how to initialize the console in Eclipse so my code could work ? Thank you

hacks4life
  • 691
  • 5
  • 16
  • 38

1 Answers1

4

This is because System.console() will return the unique Console object associated with the current Java virtual machine, if any, while eclipse does not have an unique console.

You should try to use Scanner or System.in to get the console input

locoyou
  • 1,697
  • 1
  • 15
  • 19
  • I do not understand how to replace it in my program. I have error when I do that. – hacks4life Mar 24 '14 at 12:38
  • For example, you could use `Scanner scanner = new Scanner(System.in); String password = scanner.next();` – locoyou Mar 24 '14 at 12:42
  • You can add a line like `System.out.print("Enter pin:");` before the input code. – locoyou Mar 24 '14 at 12:57
  • Yep that's what I did, my question was stupid sorry. – hacks4life Mar 24 '14 at 13:00
  • @FlorentP That's fine :) – locoyou Mar 24 '14 at 13:02
  • I was thinking, do I need to close the scanner somewhere in the code ? – hacks4life Mar 24 '14 at 13:03
  • @FlorentP When you don't use System.in, you can close it. – locoyou Mar 24 '14 at 13:08
  • That's weird, when I add a "scanner.close()" in my code, I have a java.util.NoSuchElementException at "java.util.Scanner.throwFor(Unknown Source)", at "java.util.Scanner.next(Unknown Source)", and at "String password = scanner.next()"; in my code. – hacks4life Mar 24 '14 at 13:19
  • `scanner.close()` will close `System.in` and `System.in` is static, so if you close it at some place, you cannot use it any more. – locoyou Mar 24 '14 at 13:22
  • I see, so where do you suggest me to close it ? Or I could use "scanner = new Scanner(System.in);" instead of "Scanner scanner = new Scanner(System.in);" right ? This way I don't have to close it. – hacks4life Mar 24 '14 at 13:25
  • I always use a static scanner and close it at the end of main method if more than one object needs to use it. – locoyou Mar 24 '14 at 13:29
  • And in your code, you should add `Scanner scanner = new Scanner(System.in);` out of your loop – locoyou Mar 24 '14 at 13:30
  • Ok perfect. I put the scanner outside the two do{}while loops and I have no problems anymore. Thanks again. – hacks4life Mar 24 '14 at 13:33