There are files:
- UserRepository.java
- UserService.java
- Thread.java
- Task.java

When the program starts, an error is displayed every minute (logical), that in the Task class in the line List<User> users = userServices.findAllUsers(); I have nothing "accepted", i.e. Hibernate does not want to fulfill my request. A typical error is java.lang.NullPointerException .

Question: why Hibernate does not want to take data from the users table and output data in the stream, although during normal loading of the page (in the method, @getmapping) findAllUsers() works?

Here are my files:

Application.properties:

 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=USER spring.datasource.password=PASS spring.datasource.url=jdbc:mysql://LOCALHOST ## Hibernate Properties # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.show-sql=true spring.jpa.generate-ddl=true # Hibernate DDL auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto = update spring.datasource.initialization-mode=always 

UserRepository.java:

 @Repository public interface UserRepository extends CrudRepository<User, Long> { List<User> findAll(); } 

UserService.java:

 @Service public class UserServices { private UserRepository userRepository; @Autowired public UserServices(UserRepository userRepository) { this.userRepository = userRepository; } public List<User> findAllUsers(){ return userRepository.findAll(); } } 

Threads.java:

 public class Threads { public void start() { Task task = new Task(); ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3); executorService.scheduleAtFixedRate(task::downloadCalendars, 0, 1, TimeUnit.MINUTES); } } 

Task.java:

 public class Task { private Cutter cutter = new Cutter(); @Autowired private UserServices userServices; void downloadCalendars(){ try { LocalTime time = LocalTime.now(); if(time.getHour() == 0 && time.getMinute() == 0) { List<User> users = userServices.findAllUsers(); for (User user : users) { new Thread(() -> { try { cutter.updateCalendar(user); } catch (Exception e) { e.printStackTrace(); if (user != null) { System.out.println("проблема в user " + user.getId()); } } }).start(); } } } catch (Exception e){ e.printStackTrace(); } } } 

    1 answer 1

    In this case, everything is quite simple. Hiberneyt nothing to do with. The NullPointerException error has nothing to do with the hibernate. It occurs because you call a method on an uninitialized (Null) variable. In this case, it is the variable userServices.findAllUsers (). But the question of how much this variable is not initialized already refers to understand the work of the spring. Springs have context. In essence, all the work of the spring comes down to creating a container in which bins are stored (your repositories, controllers, services, etc.) However, your Task class is not a bin (there are no annotations on it), this means that you went beyond the scope of the spring context, it does not know anything about instances of the Task class, and therefore cannot initialize your private UserServices userServices variable, which for this purpose (for automatic initialization by the spring) is marked with the @Autowired annotation. Make your class a component, create it also with the help of a spring (not through the operator new, otherwise you will get the same problem for the same reason) and you will be happy.

    In your case, remove the Thread.java , because we will not need it and will Task.java to work with Task.java .

    Add some annotations to the Task.java class:

     @Configuration @EnableScheduling public class Task { private Cutter cutter = new Cutter(); @Autowired private UserServices userServices; @Scheduled(cron = "0 30 9 * * *")//например, выполняется в 9:30 каждый день void downloadCalendars(){ ... } 

    And here you have found happiness)

    • added @Component annotation at the beginning of the Task class and did not work. Happiness has not yet found) - Antonio112009
    • And how do you create an instance of the task class? Task task = new Task (); - if so, as shown in the code, then read the answer again)) - Dmitry
    • Task task = new Task() in Thread. I tried to replace with the @Autowired Task task and, giving the Task and Thread @component classes, did not work ... - Antonio112009
    • your class public class Threads must be a component. and then the question is, how is an instance of this class created? in general, the spring has the @Scheduled annotation, and there are crowns in it. put on a method that needs to be performed on a schedule ... - Dmitry
    • Yes, I read a couple of resources already about @Schedule. Now moderation, I hope, will confirm the amendments in your answer and then there will be a jackdaw) In any case, it will be, because the answer has helped! - Antonio112009