Why does my Java program work in console and not in Eclipse? -
i wanted know why java program work in console when :
javac main.java
java main
...and not in eclipse, have error :
exception in thread "main" java.lang.nullpointerexception @ codepin.main.main(main.java:48) --> char passwordarray[] = console.readpassword("enter pin: ");
here's 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; { { char passwordarray[] = console.readpassword("enter pin: "); //this line causing error pinsize = passwordarray.length; if (pinsize != 4) { system.out.println("pin must 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()]; (int = 0; < thepins.length; i++) { thepins[i] = pins.get(i).tostring(); } string passentered = string.valueof(passwordarray); (int = 0; < 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 can see, added system.out.println(console == null);
@ beggining of public static void main method check, , return true in console.
so question : how initialize console in eclipse code work ? thank you
this because system.console()
return unique console object associated current java virtual machine, if any, while eclipse not have unique console.
you should try use scanner
or system.in
console input
Comments
Post a Comment