The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception

Code directly

1. To create a serviceutils tool class, annotate it with @ Autowired

package com.deao.app.socket.netty.server.service.impl;

import com.deao.app.socket.netty.server.service.MessageService;
import com.deao.app.socket.netty.server.service.SocketGasExamArrangeManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class ServiceUtils {
    @Autowired
    public SocketGasExamArrangeManageService socketGasExamArrangeManageService;
    @Autowired
    public MessageService messageService;
    public static ServiceUtils serviceUtils;

    @PostConstruct
    public void init() {
        serviceUtils = this;
    }

}

Used elsewhere

    public static void main(String[] args) {
        serviceUtils.socketGasExamArrangeManageService.selectGasExamArrangeManageByStudentId(studentId);
        serviceUtils.messageService.message(null, request, account, null, true);
    }

 

Read More: