[fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring




Exception Stacktrace Extract:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
[Request processing failed: java.lang.NullPointerException: 
Cannot invoke "org.code2care.todolist.todolist.entities.ToDoRepository.findById(Object)" 
because "this.toDoRepository" is null]


Reason:

The reason for this error is that you have defined a member variable of your Repository class in the Spring Controller class but have not @Autowired it. As it is not injected by the Spring IoC container, you get a NullPointerException when you try to do an operation over it



Solution:

Make sure you add @Autowired annotation above all your repositories in the Controller class,

Example:
@Autowired
private ToDoListRepository toDoListRepository;
    
@Autowired
private ToDoRepository toDoRepository;


Fix - NullPointerException Cannot Invoke findById because Repository is null


-


Have Questions? Post them here!

Top Hashtags:

Advertisements

Advertisements




Advertisements