1

I'm trying to create a user login system for an application. This login system works by allowing a user to register a profile and save it to a database. A login interface is then created in the logininterface class where the user inputs their username and password. This class should then call user service which access' a repository to locate the username and password in the database. The issue persists when trying to retrieve a user's details from the database, the repository used returns null. I think this is due to it not being initialised properly, however I cannot find the correct way to initialise the repository. Code snippets are included below:

Main class

@SpringBootApplication
public class Application implements ApplicationRunner {

    @Autowired
    UserRepo userRepo;

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {


        System.setProperty("java.awt.headless", "false");

      
        LoginInterface.createLogin();
    }
}

User Repository

@Repository
public interface UserRepo extends JpaRepository<User, Long> {
    Optional<User> findByuserName(String userName);
}

User Service

@Service
public class UserService implements UserDetailsService {


    private final static String not_found = "user with username %s not found";

    @Autowired
    public UserRepo userRepo;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userRepo.findByuserName(username).orElseThrow(() -> new UsernameNotFoundException(String.format(not_found, username)));
    }
}

Login Interface

public class LoginInterface implements ActionListener {


    private UserService userService = new UserService();

    public static void createLogin(){
        button.addActionListener((ActionListener) new LoginInterface());

      

    }

    @Override
    public void actionPerformed(ActionEvent e) {


        String user = username.getText();
        String password = username.getText();

        User theUser = (User) userService.loadUserByUsername(user);
        String theUsername = theUser.getUsername();
        String thePassword = theUser.getPassword();

        if(user.equals(theUsername) && password.equals(thePassword)){
            JOptionPane.showMessageDialog(null, "Login Successful");
        }
        else{
            JOptionPane.showMessageDialog(null, "Username or Password mismatch");
        }
    }
}
newtospring
  • 13
  • 1
  • 4

2 Answers2

0

Try injecting/autowiring the UserService instead of instantiating it manually in the LoginInterface.

karim farhouti
  • 310
  • 1
  • 7
  • When I do this, I receive the error that userservice is null. I presume this is because my logininterface class isn't a spring bean? – newtospring Apr 19 '22 at 09:01
  • No you can inject a managed spring bean in a non managed one ,so you can autowire without adding @Service ,can you show more details about the exception? – karim farhouti Apr 19 '22 at 09:09
  • The exception thrown is the following Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "softwareproject.user.UserService.loadUserByUsername(String)" because "this.userService" is null – newtospring Apr 19 '22 at 09:12
0

The problem in your code is here:

private UserService userService = new UserService();

You expect that the object will be created by the container through dependency injection, but instead, you create it manually. It does not work like that.

Try something like this:

@Service
public class LoginInterface implements ActionListener {
   //
   @Autowired
   private UserService userService
   //
}
jwpol
  • 1,188
  • 10
  • 22
  • I've implemented your code however, I get a new error of Error creating bean with name LoginInterface: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError Caused by: java.lang.ExceptionInInitializerError: null Caused by: java.awt.HeadlessException: null. I fixed this by using the following code SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class); builder.headless(false); ConfigurableApplicationContext context = builder.run(args);. However, it still states that user service is null. – newtospring Apr 19 '22 at 09:05
  • This is another story explained for example under https://stackoverflow.com/questions/51004447/spring-boot-java-awt-headlessexception. Please, find an example of how to set up a swing application with spring-boot or post another question. – jwpol Apr 19 '22 at 09:11
  • Hey, I've implemented this code. However, it still throws the same error that the user service is null. – newtospring Apr 19 '22 at 09:17