0

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

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • Please post code of `StoreFacade.login()` and mention where the line #72 that causes the `NullPointerException` – AlexR Sep 21 '14 at 13:45
  • I don't speak Java, but [this](http://stackoverflow.com/questions/6020719/what-could-cause-java-lang-reflect-invocationtargetexception) SO answer may help you to figure out the actual error message. – mgamba Sep 21 '14 at 13:46
  • `System.console()` will return null if you aren't actually running the program from a console. – President James K. Polk Sep 21 '14 at 13:47
  • 1
    the function i have posted is the StoreFacade.login function, and i found out that the reason is quite different. the reason is that System.console() returns null, do you have any idea how can i read a password from the console without using System.console()? – vcmkrtchyan Sep 21 '14 at 13:48
  • @GregS, does that mean that the final program will run normal? – vcmkrtchyan Sep 21 '14 at 13:48
  • @Carmine: try it and see. – President James K. Polk Sep 21 '14 at 13:49
  • It is an [xy problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – afzalex Sep 21 '14 at 14:15

0 Answers0