Query in Spring Boot retuns null

java.lang.NullPointerException. I'm trying to get user id by his username. I use MySQL,Thymeleaf

Please help


    import javax.persistence.*;
    import java.util.Objects;
    @Table(name = "users")
    public class Users {
        private int id;
        private String Username;
        private String password;
        private Byte active;
        private String role;
        @Column(name = "id", nullable = false)
        public int getId() {
            return id;
        public void setId(int id) {
            this.id = id;
        @Column(name = "username", nullable = false)
        public String getUsername() {
            return Username;
        public void setUsername(String username) {
            this.Username = username;
        @Column(name = "password", nullable = false)
        public String getPassword() {
            return password;
        public void setPassword(String password) {
            this.password = password;
        @Column(name = "role", nullable = false)
        public String getRole() {
            return role;
        public void setRole(String role) {
            this.role = role;
        @Column(name = "active", nullable = true)
        public Byte getActive() {
            return active;
        public void setActive(Byte active) {
            this.active = active;
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Users users = (Users) o;
            return id == users.id && Objects.equals(password, users.password) && Objects.equals(role, users.role) && Objects.equals(Username, users.Username) && Objects.equals(active, users.active);
        public int hashCode() {
            return Objects.hash(id, Username, password, role, active);


    package com.example.accessingdatamysql.controllers;
    import com.example.accessingdatamysql.models.Request;
    import com.example.accessingdatamysql.models.Users;
    import com.example.accessingdatamysql.repo.RequestRepo;
    import com.example.accessingdatamysql.repo.UserRepo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.*;
    import java.security.Principal;
    public class MainController {
        RequestRepo requestRepo;
        UserRepo userRepo;
        Principal principal;
        @GetMapping(path = "/")
        public String main(Model model, Principal principal) {
            Iterable requests =  requestRepo.findAll();
            model.addAttribute("user", principal.getName());
            return "main";
        @PostMapping(path = "/request/add")
        public String RequestAdd(Principal principal,Model model,@RequestParam long department,long category_id ) {
           Users user = userRepo.findUserByUsername(principal.getName());
            Request request = new Request(department,category_id, user.getId());
            return "redirect:/";
        @GetMapping(path = "/delete/{id}")
        public String RequestDetails(Model model, @PathVariable(value = "id") long id) {
            return "redirect:/";


    package com.example.accessingdatamysql.repo;
    import com.example.accessingdatamysql.models.Users;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.data.repository.query.Param;
    public interface UserRepo extends CrudRepository {
        @Query("select e from Users e where e.username = :username")
        Users findUserByUsername(@Param("username") String username);

Read more here: https://stackoverflow.com/questions/68478154/query-in-spring-boot-retuns-null

Content Attribution

This content was originally published by stalker at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: