In my program, the login function is called using reflection, and for the password reading line it throws InvocationTargetException, I used System.console().readPassword method to read the password from the console, because i want to hide the password from the user, here is my code
public void login() {
Scanner input = new Scanner(System.in);
UserList users = null;
try {
JAXBContext context = JAXBContext.newInstance(UserList.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
users = (UserList) unmarshaller.unmarshal(new File("./src/xml/Users.xml"));
}catch (JAXBException e){
e.printStackTrace();
System.exit(0);
}finally {
if(users != null){
System.out.println("Please enter your login");
String login = input.nextLine();
User requestedUser = null;
for (User user : users.getUsers()){
if(user.getLogin().trim().equals(login)){
requestedUser = user;
break;
}
}
if(requestedUser != null){
String password = new String(System.console().readPassword("Please enter your password: "));
if(password.equals(requestedUser.getPassword().trim())){
Session.getInstance().setLoggedInUser(requestedUser);
}else{
System.out.println("Wrong password");
}
}else{
System.out.println("User not found");
}
}
}
}
and here is my exception stacktrace
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at Session.run(Session.java:82)
at Main.main(Main.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.NullPointerException
at StoreFacade.login(StoreFacade.java:72)
... 11 more
please explain what's wrong here