使用@autowire将spring bean注入组件时出现空指针异常

时间:2018-04-11 03:01:12

标签: java spring nullpointerexception javabeans

我是春天的新手。 当我尝试将bean fileService 注入组件类 AuditExtractor 时,但当我在fileService中调用行中的函数时,它会抛出Null Pointer Exception fileService.getExistingFile()我尝试了很多不同的方法,但是他们没有工作。 以下是我的代码:

AuditExtractor.java

@Component
@Configurable 
public class AuditExtractor {
        private final AuditReportExportDTO auditReportExportDTO;
        private static final String CHECKBOX_CHECKED = "<fo:inline font-size=\"11pt\" border=\"1pt black \">&#x2714;</fo:inline>";
        private static final String CHECKBOX_UNCHECKED = "<fo:inline font-size=\"12pt\">&#x274F;</fo:inline>\n";
        private final String ONE_BLOCK = "<fo:block>%s</fo:block>";
        private final EscapeTool esc = new EscapeTool();
        private final Map<String, QAndADTO> questionMap;
        private final List<AuditFindingSvcDTO> auditFindings;

    @Autowired
    FileAttachmentService fileService;

    public AuditExtractor() {
        questionMap = new HashMap<>();
        auditFindings = new ArrayList<>();
        auditReportExportDTO = new AuditReportExportDTO();
    }

    public AuditExtractor(AuditReportExportDTO auditReportExportDTO) {
        auditFindings = auditReportExportDTO.getAuditFindings();
        this.auditReportExportDTO = auditReportExportDTO;
        questionMap = new HashMap<>();
        auditReportExportDTO.getQuestionnarieAnswers().stream().filter(a -> a.getQuestion().getQuestionCode() != null)
                .forEach(a -> questionMap.put(a.getQuestion().getQuestionCode(), a));
    }

public String getDisplayPhoto(String questionCode) throws IOException {
            QAndADTO questionAndAnswer = questionMap.get(questionCode);
            String siteCode = auditReportExportDTO.getSite().getCode();
            String identifier = "";
            if (questionAndAnswer != null && questionAndAnswer.getAnswer() != null && questionAndAnswer.getAnswer().getAnswerFileAttachments().size() > 0) {
                identifier = questionAndAnswer.getAnswer().getAnswerFileAttachments().get(0).getIdentifier();
                AttachmentDTO fileDetails = fileService.getExistingFile(siteCode, identifier);
                if (fileDetails != null) {
                    InputStream is = fileDetails.getFileContents();
                    byte[] bytes = IOUtils.toByteArray(is);
                    String encoded = Base64.getEncoder().encodeToString(bytes);
                    return "url(&quot;data:image/png;base64," + encoded + "&quot;)";
                }
            }
            return "";
        }

FileAttachmentService.java

public interface FileAttachmentService {

void delete(String entityCode, String identifier) throws IOException;

List<AttachmentDTO> getFileListForEntity(String entityCode) throws IOException;

AttachmentDTO getExistingFile(String entityCode, String identifier) throws IOException;

String upload(String entityCode, String filename, String contentType, long fileSize, InputStream file) throws IOException;

AnswerFileAttachmentDTO uploadAuditDetailFile(String entityCode, String questionCode, String questionVersion, String questionnaireCode, String filename, String contentType, long fileSize, InputStream file) throws IOException;

ResponseEntity<Void> deleteAuditDetailFile(String entityCode, String questionCode, String questionVersion, String questionnaireCode, String identifier) throws IOException;
}

FileAttachmentServiceImpl.java

@Service
public class FileAttachmentServiceImpl implements FileAttachmentService {

private final com.sedex.spectrum.common.attachment.service.attachment.AttachmentService fileService;
private final SecurityService securityService;
private final AuditService auditService;

@Autowired
public FileAttachmentServiceImpl(final com.sedex.spectrum.common.attachment.service.attachment.AttachmentService fileService, final SecurityService securityService, final AuditService auditService) {
    this.fileService = fileService;
    this.securityService = securityService;
    this.auditService = auditService;
}

@Override
public void delete(final String entityCode, final String identifier) throws IOException {
    fileService.deleteFile(entityCode, identifier);
    auditService.attachmentAudit(entityCode, AuditAttachmentsAuditAction.DELETE, identifier);
}

@Override
public ResponseEntity<Void> deleteAuditDetailFile(String entityCode, String questionCode, String questionVersion, String questionnaireCode, String identifier) throws IOException {
    auditService.deleteAuditDetailAttachment(entityCode, questionCode, questionVersion, questionnaireCode, identifier);
    fileService.deleteFile(entityCode, identifier);
    return ResponseEntity.ok().build();
}

@Override
public List<AttachmentDTO> getFileListForEntity(final String entityCode) throws IOException {
    return fileService.getFileListForEntityCode(entityCode);
}

@Override
public AttachmentDTO getExistingFile(final String entityCode, final String identifier) throws IOException {
    return fileService.downloadFile(entityCode, identifier);
}

@Override
public String upload(final String entityCode, final String filename, final String contentType, final long fileSize, final InputStream file) throws IOException {
    AttachmentDTO fileAttributes = new AttachmentDTO();
    fileAttributes.setEntityCode(entityCode);
    fileAttributes.setFileName(filename);
    fileAttributes.setContentType(contentType);
    fileAttributes.setSize(fileSize);
    fileAttributes.setUpdatedBy(securityService.getUserWithAuthorities().getUserCode());
    String identifier = fileService.addFile(file, fileAttributes);
    auditService.attachmentAudit(entityCode, AuditAttachmentsAuditAction.UPLOAD, identifier);
    return identifier;
}

@Override
public AnswerFileAttachmentDTO uploadAuditDetailFile(String entityCode, String questionCode, String questionVersion, String questionnaireCode, String filename, String contentType, long fileSize, InputStream file) throws IOException {
    AttachmentDTO fileAttributes = new AttachmentDTO();
    fileAttributes.setEntityCode(entityCode);
    fileAttributes.setFileName(filename);
    fileAttributes.setContentType(contentType);
    fileAttributes.setSize(fileSize);
    fileAttributes.setUpdatedBy(securityService.getUserWithAuthorities().getUserCode());
    String identifier = fileService.addFile(file, fileAttributes);
    return auditService.attachmentAuditDetail(entityCode, questionCode, questionVersion, questionnaireCode, identifier, filename);
}
}

1 个答案:

答案 0 :(得分:0)

在FileAttachmentServiceImpl中,添加带参数

的构造函数
public FileAttachmentServiceImpl(final com.sedex.spectrum.common.attachment.service.attachment.AttachmentService fileService, final SecurityService securityService, final AuditService auditService) {
    this.fileService = fileService;
    this.securityService = securityService;
    this.auditService = auditService;
}

默认构造函数是在类定义中没有构造函数时创建的。如果添加构造函数,则不会添加默认构造函数。

@Autowired将使用没有参数的构造函数创建bean,因此您还需要添加不带参数的构造函数

public FileAttachmentServiceImpl() {
    // add init code here
}

此外,@ Aututired上面的构造函数不是必需的。