I think your problem is related with the lack of a @Component annotation in your BoardDao class.
The component should have a @Component annotation to instantiate the singleton to be injected in your Service Layer.
@Component
public class BoardDao extends SqlSessionDaoSupport{
@Autowired
SqlSessionTemplate session;
public List<BoardDto> listboard(BoardDto dto) {
System.out.println("dao.");
List<BoardDto> result = session.selectList("boarddate.listboard", dto);
return result;
}
}
If the problem persist, you may try with the @Repository annotation. Sadly I haven't used the class SqlSessionDaoSupport, so I don't know exactly the best annotation for that.
I think your problem is related with the lack of a @Component annotation in your BoardDao class.
The component should have a @Component annotation to instantiate the singleton to be injected in your Service Layer.
@Component
public class BoardDao extends SqlSessionDaoSupport{
@Autowired
SqlSessionTemplate session;
public List<BoardDto> listboard(BoardDto dto) {
System.out.println("dao.");
List<BoardDto> result = session.selectList("boarddate.listboard", dto);
return result;
}
}
If the problem persist, you may try with the @Repository annotation. Sadly I haven't used the class SqlSessionDaoSupport, so I don't know exactly the best annotation for that.
In my case it was with a Map, I wanted to increase its value, but this initial one will not exist, therefore it is null.
map.put(0, map.get(0) 1);
It was fixed by adding a validation, like so:
map.put(0, map.get(0) != null ? map.get(0) + 1 : 1);
java - Spring Boot Unit test gives error: Cannot invoke "" because "this.taskService" is null - Stack Overflow
Spring Boot Test cannot give a clear message but throw NPE now when @SpringBootConfiguration cannot be found
Cannot invoke "java.lang.Class.isInterface()" because "typeToRead" is null
spring boot - java.lang.NullPointerException: Cannot invoke because the return value of is null - Stack Overflow
This error occurs as the required bean is not autowired. In this case, if we autowire customerRepository, the problem will be resolved:
@Autowired
CustomerRepository customerRepository ;
CustomerRepository is not injected as a Bean that's why this exception is coming. If CustomerRepository is Interface then try with constructor based bean injection. In my case, it worked when Constructor was invoked.
You're probably missing the @repository annotation on top of your repository class.
Another unrelated word of advice:
In your controller you use findAll and filter in java to keep only the ids. Then you go to the same repository and perform another query per user-id from above. This is a causing you to create multiple database calls which are one of the most expensive operations you can do, when you already have all your data from the first single query...
Also if you're only looking at the bottom part of the function you don't event need a query per each user-id (when you have a list of user ids as input), you can create a query that uses the 'in' convention and pass a list of user-ids to create a single db call.
First of all I would get rid of @Autowired ICustomerRepository customerRepository; in UserList class. It doesn't belong there. The counting of linked customers should be executed in ICustomerRepository and the result to be passed into UserList via the constructor.
e.g.
public class UserList {
private String id;
private String email;
private String userType;
private String rolls;
private String partner;
private Long customersLinked; //better use Long instead of Integer
private String position;
private String status;
// constructor takes the number of linked customers as parameter
public UserList (Users user, Long customersLinked ) {
this.id = user.getId();
this.email = user.getEmail();
this.userType = user.getUserType();
this.rolls = user.getRolls();
this.partner = user.getPartner();
this.customersLinked = customersLinked;
this.position = user.getPosition();
this.status =user.getStatus();
}
//Getter and Setter
}
and then create the count query in ICustomerRepository
e.g.
public interface ICustomerRepository extends MongoRepository<Customer, String> {
//other methods
Long countByLinkedUsersIn(String id); //not so sure if this query works in mongo
}
and finally in your controller
Optional<Users> user = this.usersRepository.findById(userId);
Long count = this.usersRepository.countByLinkedUsersIn(userId);
userList.add(new UserList(user.get(), count));
P.S. I have a doubt for the query method: Long countByLinkedUsersIn(String id);. Usually when repository methods have "In" in their names, countByLinkedUsersIn, then it is expected as parameter a List and not a single user id. However if your previous method List<Customer> findAllByLinkedUsersIn(String id); worked for you, then this one should work too.
The solution is simple. You mocked a class customerDataRepository but did not instruct it the mock what to do if the corresponding method is called. Mockito mocks then default back on doing nothing by method call and if there is a return value return null.
Since your returned customerData is null you get your NPE when calling on this object. In your case this is in the error case that you get by calling getCustomerId().
To solve this issue simply instruct your mock
@Test
void removeCustomerDataWhenConsentIsNotGiven() {
CustomerData customerDataTest = customerData;
//when
Mockito.when(customerDataRepository.findCustomerByDialogId(Mockito.any())).thenReturn(new CustomerData()); // <-- Add this line
customerDataService.giveConsent(false,22L);
//then
verify(customerDataRepository,times(1)).save(customerDataTest);
}
you can obviously replace Mockito.any() with Mockito.anyInt() or 42 and new CustomerData() with a object you previously created. I think you get the idea ;)
Assuming that you have just corrected method names before posting it to Stackoverflow, and method you are calling in the test: giveConsent is, actually, the same method as methodTotest of the CustomerDataService.
Before calling customerDataService.giveConsent(false,22L);, you need to configure you repository to return some test (not null! or mocked) customerData entity:
when(customerDataRepository.findCustomerByDialogId(22L)).thenReturn(customerDataTest);
customerDataService.giveConsent(false,22L);
Note: since you are passing false as 1st variable, you will get to this branch of code
if (!consent) {
customerDataRepository.deleteById(customer.getCustomerId());
}
And in the test you are expecting save() method to be called, so the test will fail.
I am new to spring boot and I am wondering how to allow a response JSON to include null values for database columns that are nullable. This is my controller, service, repository and model:
package com.findersgame.questtracker.controller;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.findersgame.questtracker.model.MapArea;
import com.findersgame.questtracker.service.MapAreaService;
@RestController
public class MapAreaController {
private MapAreaService mapAreaService;
public MapAreaController(MapAreaService mapAreaService) {
super();
this.mapAreaService = mapAreaService;
}
@PostMapping("selected_map_areas")
public List<MapArea> selectedMapAreas(@RequestBody List<Integer> selectedMapAreas) {
return mapAreaService.selectedMapAreas(selectedMapAreas);
}
}
package com.findersgame.questtracker.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.findersgame.questtracker.model.MapArea;
import com.findersgame.questtracker.repository.MapAreaRepository;
@Service
public class MapAreaService {
private MapAreaRepository mapAreaRepository;
public MapAreaService(MapAreaRepository mapAreaRepository) {
super();
this.mapAreaRepository = mapAreaRepository;
}
public List<MapArea> selectedMapAreas(List<Integer> selectedIds) {
return mapAreaRepository.findAllById(selectedIds);
}
}
package com.findersgame.questtracker.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.findersgame.questtracker.model.MapArea;
public interface MapAreaRepository extends JpaRepository<MapArea, Integer> {
}
package com.findersgame.questtracker.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "map_areas")
public class MapArea {
@Id
private Integer id;
@Column
private Integer mapTabId;
@Column
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getMapTabId() {
return mapTabId;
}
public void setMapTabId(Integer mapTabId) {
this.mapTabId = mapTabId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My problem is that when I call http://localhost:8080/selected_map_areas with body [14, 15, 16] I get the following error.
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Cannot invoke "java.lang.Integer.intValue()" because "this.mapTabId" is null]
I guess it makes sense because in the database, the map_tab_id value is null for both id 15 and 16. I'm guessing that I'm missing an annotation in one of the classes/interfaces above but I have not been able to find which one.
Note that it works fine and I get the correct result in Postman when `map_tab_id` has a value.
Please help.
java.lang.NullPointerException: Cannot invoke "com.example.demo.student.StudentRepository.findAll()" because "this.studentRepository" is null
I do have a @Repository annotated above my StudentRepository class. I also used @Autorwired above the constructor in my StudentService class. I think the problem is with dependency injection but I don't see why.
Related code:
//StudentRepository.java
package com.example.demo.student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
}
//StudentService.java
package com.example.demo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
@Service
public class StudentService {
private final StudentRepository studentRepository;
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public List<Student> getStudents() {
return studentRepository.findAll();
}
}
//StudentController.java
package com.example.demo.student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> getStudents(StudentService studentService) {
return studentService.getStudents();
}
}I'm pretty new at Java and was trying to do a CRUD, the process was going pretty smoothly until i had to make the create function to the table with a composite key.
At first i thought the problem was with the Dependecy Injection, but it's working just fine in the methods getByStateAndModelId() and getAll().I also tried to set the names for the EquipmentModel and EquipmentState in the createEmshe (In the POST i just insert the Id of both State and Models, and the Value, so the names from Model and State coming from the DTO are null and i thought that maybe that was the cause). But then, both the equipmentModelRepository and the equipmentStateRepository returned the NullPointerException too, what i'm missing?here's the relevant code:
The service:
package com.api.forestoperation.emshe;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.api.forestoperation.equipmentmodel.EquipmentModelModel;
import com.api.forestoperation.equipmentmodel.EquipmentModelRepository;
import com.api.forestoperation.equipmentstate.EquipmentStateModel;
import com.api.forestoperation.equipmentstate.EquipmentStateRepository;
@Service
public class EquipmentModelStateHourlyEarningsService {
@Autowired
EquipmentModelStateHourlyEarningsRepository emsheRepository;
@Autowired
EquipmentModelRepository equipmentModelRepository;
@Autowired
EquipmentStateRepository equipmentStateRepository;
public List<EquipmentModelStateHourlyEarningsModel> getAllEmshe() {
return emsheRepository.findAll();
}
public EquipmentModelStateHourlyEarningsModel createEmshe(EquipmentModelStateHourlyEarningsDTO emsheDTO) {
var emsheModel = new EquipmentModelStateHourlyEarningsModel();
BeanUtils.copyProperties(emsheDTO, emsheModel);
EquipmentModelModel emsheModelInfo = emsheModel.getId().getEquipmentModel();
EquipmentStateModel emsheStateInfo = emsheModel.getId().getEquipmentState();
EquipmentModelStateHourlyEarningsPK emshePk = new EquipmentModelStateHourlyEarningsPK(emsheModelInfo,
emsheStateInfo);
emsheModel.setId(emshePk);
return emsheRepository.save(emsheModel);
}
public EquipmentModelStateHourlyEarningsModel getEmsheByStateAndModelId(UUID modelId, UUID stateId) {
var modelExists = equipmentModelRepository.findById(modelId).orElse(null);
var stateExists = equipmentStateRepository.findById(stateId).orElse(null);
if (modelExists != null && stateExists != null) {
EquipmentModelStateHourlyEarningsPK emshePk = new EquipmentModelStateHourlyEarningsPK(modelExists,
stateExists);
EquipmentModelStateHourlyEarningsModel emsheModel = emsheRepository.findById(emshePk).orElse(null);
return emsheModel;
}
return null;
}
}The Controller:
package com.api.forestoperation.emshe;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EquipmentModelStateHourlyEarningsController {
@Autowired
EquipmentModelStateHourlyEarningsService emsheService;
@PostMapping("/equipment-model-state-hourly-earnings")
public ResponseEntity<Object> saveEmshe(@RequestBody EquipmentModelStateHourlyEarningsDTO emsheDTO) {
var savedEmshe = new EquipmentModelStateHourlyEarningsService().createEmshe(emsheDTO);
return savedEmshe != null
? ResponseEntity.status(HttpStatus.CREATED)
.body("EquipmentModelStateHourlyEarnings created with Sucess")
: ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
@GetMapping("/equipment-model-state-hourly-earnings")
public ResponseEntity<List<EquipmentModelStateHourlyEarningsModel>> getAllEmshe() {
List<EquipmentModelStateHourlyEarningsModel> equipments = emsheService.getAllEmshe();
return ResponseEntity.status(HttpStatus.OK).body(equipments);
}
@GetMapping("/equipment-model-state-hourly-earnings/{modelId}/{stateId}")
public ResponseEntity<Object> getEmsheByModelAndStateId(@PathVariable(value = "modelId") UUID modelId,
@PathVariable(value = "stateId") UUID stateId, EquipmentModelStateHourlyEarningsPK emshePk) {
EquipmentModelStateHourlyEarningsModel emsheModel = emsheService.getEmsheByStateAndModelId(modelId, stateId);
return emsheModel == null ? ResponseEntity.status(HttpStatus.BAD_REQUEST).body("EMSHE nulo")
: ResponseEntity.status(HttpStatus.OK).body(emsheModel);
}
}The Repository:
package com.api.forestoperation.equipment;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EquipmentRepository extends JpaRepository<EquipmentModel, UUID> {
}The Model:
package com.api.forestoperation.emshe;
import java.io.Serializable;
import com.api.forestoperation.equipmentmodel.EquipmentModelModel;
import com.api.forestoperation.equipmentstate.EquipmentStateModel;
import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@Entity
@Table(name="equipment_model_state_hourly_earnings", schema="operation")
public class EquipmentModelStateHourlyEarningsModel implements Serializable {
@EmbeddedId
private EquipmentModelStateHourlyEarningsPK id;
private static final long serialVersionUID = 1L;
@Column(name="value")
private double value;
public EquipmentModelStateHourlyEarningsPK getId() {
return id;
}
public void setId(EquipmentModelStateHourlyEarningsPK id) {
this.id = id;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public EquipmentModelModel getEquipmentModel() {
return id.getEquipmentModel();
}
public EquipmentStateModel getEquipmentState() {
return id.getEquipmentState();
}
}The Pk:
package com.api.forestoperation.emshe;
import java.io.Serializable;
import java.util.Objects;
import com.api.forestoperation.equipmentmodel.EquipmentModelModel;
import com.api.forestoperation.equipmentstate.EquipmentStateModel;
import jakarta.persistence.Embeddable;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
@Embeddable
public class EquipmentModelStateHourlyEarningsPK implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinColumn(name = "equipment_model_id")
private EquipmentModelModel equipmentModel;
@ManyToOne
@JoinColumn(name = "equipment_state_id")
private EquipmentStateModel equipmentState;
public EquipmentModelStateHourlyEarningsPK() {
}
public EquipmentModelStateHourlyEarningsPK(EquipmentModelModel equipmentModelModel,
EquipmentStateModel equipmentStateModel) {
this.equipmentModel = equipmentModelModel;
this.equipmentState = equipmentStateModel;
}
@Override
public int hashCode() {
return Objects.hash(equipmentModel, equipmentState);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EquipmentModelStateHourlyEarningsPK other = (EquipmentModelStateHourlyEarningsPK) obj;
return Objects.equals(equipmentModel, other.equipmentModel)
&& Objects.equals(equipmentState, other.equipmentState);
}
public EquipmentModelModel getEquipmentModel() {
return equipmentModel;
}
public void setEquipmentModel(EquipmentModelModel equipmentModel) {
this.equipmentModel = equipmentModel;
}
public EquipmentStateModel getEquipmentState() {
return equipmentState;
}
public void setEquipmentState(EquipmentStateModel equipmentState) {
this.equipmentState = equipmentState;
}
}Here's the error i get:
java.lang.NullPointerException: Cannot invoke "com.api.forestoperation.emshe.EquipmentModelStateHourlyEarningsRepository.save(Object)" because "this.emsheRepository" is null at com.api.forestoperation.emshe.EquipmentModelStateHourlyEarningsService.createEmshe(EquipmentModelStateHourlyEarningsService.java:39) ~[classes/:na] at com.api.forestoperation.emshe.EquipmentModelStateHourlyEarningsController.saveEmshe(EquipmentModelStateHourlyEarningsController.java:22) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-6.0.13.jar:6.0.13] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-6.0.13.jar:6.0.13] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.13.jar:6.0.13] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.13.jar:6.0.13] at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590) ~[tomcat-embed-core-10.1.15.jar:6.0] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.13.jar:6.0.13] at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) ~[tomcat-embed-core-10.1.15.jar:6.0] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-websocket-10.1.15.jar:10.1.15] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.13.jar:6.0.13] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.13.jar:6.0.13] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.13.jar:6.0.13] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.13.jar:6.0.13] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.13.jar:6.0.13] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.13.jar:6.0.13] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.15.jar:10.1.15] at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]
@Autowired annotation should be placed on each field that needs injection.
@Autowired
private UserRepository repo;
@Autowired
private UserService userService;
You are missing the @Autowired annotation before private UserService userService;.
It should be
@Autowired
private UserRepository repo;
@Autowired
private UserService userService;