0

I have nullPointerException on my databaseNotificationServices object and I don't know why. I tried with @Service and @Component annotations on class, but autowired returns null object. Do you know where is the problem?

@Service
public class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler {
    @Autowired
    private DatabaseNotificationServices databaseNotificationServices;
    static final Logger LOG = LoggerFactory.getLogger(MyAsyncUncaughtExceptionHandler.class);


    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        LOG.error("Threw exception in async method Method Name:: " + method.getName() + " " + ErrorExceptionBuilder.buildErrorResponse(ex));
        try {
            databaseNotificationServices.createNotificationRole(UserRole.ADMIN, NotificationType.ERROR,  "Error durign conversion of dat in mat file, please check the log file!", "Mat conversion error");
        } catch (QueryException e) {
            ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
            LOG.error("Threw exception in ZipAndMat::createZipAndMat, problem with notification: " + errorResponse.getStacktrace());
        }
    }
} 
luca
  • 3,248
  • 10
  • 66
  • 145
  • Should the poblem due to asynchronous task? In the other class I use @Autowired without problem – luca May 02 '16 at 15:39
  • The problem is likely due to you instantiating `MyAsyncUncaughtExceptionHandler` yourself instead of getting it from the context. Until you show us otherwise, we can only assume that is your issue and the duplicate explains the reason. – Sotirios Delimanolis May 02 '16 at 15:41
  • Yes, to set asynchronous exception handle I have in my configuration class public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new MyAsyncUncaughtExceptionHandler(); } – luca May 02 '16 at 15:47
  • 1
    Well, there's your problem. See the duplicate which explains why that's a problem and how to fix it. – Sotirios Delimanolis May 02 '16 at 15:48

0 Answers0