1

I want to create a login method for my spring application. But when I try to call the getUserByAuthentication method, I get a null pointer exception. Here is my code: Function calling the error:

@PostMapping(path = "/online")
public ResponseEntity<?> onlineRequest(@RequestBody OnlineRequest onlineRequest) {
    User user = null;
UserManager userManager = new UserManager();
    user = userManager.getUserByAuthentication(onlineRequest.getUsername(), onlineRequest.getPassword());
    if (user!=null){
        user.setLatestTimeStamp(System.currentTimeMillis());
        return new ResponseEntity<>("You are now online, Enjoy!", HttpStatus.OK);
    } else {
        return new ResponseEntity<>("Invalid login", HttpStatus.valueOf(403));
    }
}

Get User by Authentication class:

public class UserManager {
    @Autowired
    private UserRepository userRepository;
    public User getUserByID(int id){
        return userRepository.findById(id).get();
    }
    public User getUserByAuthentication(String name, String password){
        Iterable<User> userList = userRepository.findAll();
        ArrayList<User> users = new ArrayList<>();
        userList.forEach(users::add);
        User user = null;
        for (User u : users){
            if (u.getUsername().equals(name) && u.getPassword().equals(password)){
                user = u;
            }
        }
        return user;
    }
}

Repository:

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}

User class:

@Entity
@Table
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    //Initialize
    private String username;
    private String password;
    private boolean hasAccess;
    ArrayList<Integer> inChannels;
    long latestTimeStamp;

    public long getLatestTimeStamp() {
        return latestTimeStamp;
    }

    public void setLatestTimeStamp(long latestTimeStamp) {
        this.latestTimeStamp = latestTimeStamp;
    }

    public ArrayList<Integer> getInChannels() {
        return inChannels;
    }

    public void setInChannels(ArrayList<Integer> inChannels) {
        this.inChannels = inChannels;
    }

    public Long getId() {
        return id;
    }

    public User() {
    }

    public boolean hasAccess() {
        return hasAccess;
    }

    public void setAccess(boolean hasAccess) {
        this.hasAccess = hasAccess;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Alex_X1
  • 168
  • 10
  • 2
    Check [this](https://stackoverflow.com/questions/1018797/can-you-use-autowired-with-static-fields) and [this](https://stackoverflow.com/questions/10938529/why-cant-we-autowire-static-fields-in-spring/10944781) – AddeusExMachina Nov 07 '22 at 09:17
  • Yea, the static was just to test if it helped thanks – Alex_X1 Nov 07 '22 at 09:23
  • if you create the user manager yourself using `new UserManager();`, the `@Autowired` inside of it will have no effect. Let spring inject the instance for you instead. – f1sh Nov 07 '22 at 09:37
  • One hint that is not directly your question. But never store passwords of users in clear text. Please use the Spring PasswordEncoder and save and compare the encoded password strings. – Tr1monster Nov 07 '22 at 09:51
  • I know I shouldn't store passwords like that. I will add an encryption at a later point once the code itself works. – Alex_X1 Nov 07 '22 at 11:05

2 Answers2

2

You can't use @Autowired without one of the annotation that define a component in spring, so in your case, you can use @Service on UserManager like this:

@Service
public class UserManager {

and also don't use static on your method, you have to inject the UserManager component in the controller, as you do with your repository:

@Autowired
private UserManager userManager;

Then you can use:

user = userManager.getUserByAuthentication(onlineRequest.getUsername(), onlineRequest.getPassword());
       ^^^^^^^^^^^
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

I fixed it by adding the find function into the repository interface.

Alex_X1
  • 168
  • 10