Home > OS >  My repository in my spring-boot application is returning null
My repository in my spring-boot application is returning null

Time:10-07

Hey I am pretty new to using the Spring Framework and was just trying to get an application to work for practice but I am getting the following error in my stack trace: Cannot invoke "com.gabriel.schoolapp.repository.UsersRepo.findAll()" because "this.usersRepo" is null

This is my model layer for the Users:

package com.gabriel.schoolapp.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "users")

public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer usersID;
    
    @Column(nullable = false)
    private String firstName;

    public void FirstName(String firstName){
        this.firstName = firstName;
    }

    @Column(nullable = false)
    private String lastName;

    public void ClassDesc(String lastName){
        this.lastName = lastName;
    }

    @Column(nullable = false)
    private String username;

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

This is my repository layer:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.gabriel.schoolapp.models.Users;

@Repository
public interface UsersRepo extends JpaRepository<Users, Integer>{

    Users findByUsername(String firstName);
    
}

And this is my service layer:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gabriel.schoolapp.models.Users;
import com.gabriel.schoolapp.repository.UsersRepo;

@Service
public class UsersService {
    @Autowired
    private UsersRepo usersRepo;

    
    @Autowired
    public UsersService(UsersRepo usersRepo){
        
        this.usersRepo = usersRepo;
    }

    public UsersService(){

    }

     public Users createUser(Users user){
        Users checkIfUserInDb = usersRepo.findByUsername(user.getFirstName());

        if(checkIfUserInDb != null){
            System.out.println("Username already in DB");
            return null;
        }
        System.out.println("User is valid");
        return usersRepo.save(user);
    } 

    public List<Users> getAllUsersById(){
        return this.usersRepo.findAll();
    }
}

Whenever I try to call a method from the service layer like so:

@SpringBootApplication
public class SchoolAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchoolAppApplication.class, args);
        
        UsersService serv = new UsersService();
        serv.getAllUsersById();
        
    }

}

It returns with the aforementioned error (this.usersRepo is null)

Any help would be greatly appreciated thank you!!

CodePudding user response:

The UserService you created is not managed by Spring, hence the repo is not autowired. You need a few things:

  1. implement CommandLineRunner in SchoolAppApplication
  2. Autowire your service in SchoolAppApplication
  3. Override method run in SchoolAppApplication and run your service there:
    @Override
    public void run(String... args) {
        serv.getAllUsersById();
    }
  • Related