相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

My scheduled task is as follows But the application does not respond when i use @Scheduled(cron="0 46 17 * * *") And when I use @Scheduled(fixedRate = 50000) , When saving information on mongoDB The program gives an error

[ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.

: An unexpected error occurred during scheduled work. java.lang.NullPointerException

please help me for fix my cod .

@Scheduled(cron="0 46 17 * * *") // @Scheduled(fixedRate = 5000) @RequestMapping("/closeAttendance}") public void cloceAttendance() { System.out.println("-1"); AttendanceService attendanceService=new AttendanceService(); Date date=attendanceService.getCurrentDate(); System.out.println("1"); List<Attendance> attendances=arepo.findByDate(date); if(attendances!=null) { System.out.println("attendances"+attendances.size()); System.out.println("2"); for(Attendance attendance:attendances) { System.out.println("3"); attendance=attendanceService.closeAttendance(attendance); System.out.println("4"); System.out.println("attendance"+attendance.getDate()+" "+ attendance.getPerson()); // arepo.save(attendance); With the cron expression it will only run once a day. The fixed rate will run every 50 seconds. Let me guess the NullPointerException is right after the line where you create a new AttendanceService which should probably be injected by Spring instead of a new instance created by you. M. Deinum Apr 23, 2019 at 17:11

There is nothing wrong with your code. The only thing that is missing is the annotation @EnableScheduling .

Use this annotation at class level and your code will work fine.

Please don't forgot to up-vote if this solution works for you.

I used this annotation to main class And I used the @Scheduled in the my controller But the program does not run with the @Scheduled(cron="0 46 17 * * *") , but runs with the @Scheduled(fixedRate = 50000) But in both cases the arepo.save(attendance); is not running marziye Apr 23, 2019 at 10:23 error is: [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task. marziye Apr 23, 2019 at 10:29 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling ; import org.springframework.scheduling.annotation.Scheduled; com.winner.etyping.domain.RegParentDomain; import com.winner.etyping.domain.SchoolRegistrationDomain; import com.winner.etyping.domain.StudentDomain; @EnableScheduling @Servic public class StudentServiceImpl implements StudentService{ @Autowired private ClassService classserice; @Autowired private DivisionService div; @Autowired private RegParentService parent; @Autowired private SchoolregistrationService school; @Autowired private StudentDao studendao; @Transactional public List<StudentDomain> getByClassAndDivId(Integer classid, Integer div,Integer school) { return studendao.getByClassAndDivId(classid,div,school); **@Scheduled(cron="0 0 10 * * ?")** @Transactional public void sendBirthdayWish() throws Exception { studendao.sendBirthdayWish(); @Transactional public void sendNotificationToClassStudent(ClassDomain classid, SchoolRegistrationDomain schoolid , String Notification) throws Exception { studendao.sendNotificationToClassStudent(classid,schoolid,Notification);

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .

 
推荐文章