在我的程序中,使用反射调用登录函数,并且对于密码读取行,它抛出InvocationTargetException,我使用System.console()。readPassword方法从控制台读取密码,因为我想隐藏密码来自用户,这是我的代码
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");
}
}
}
}
这是我的异常堆栈跟踪
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
请解释这里有什么问题