Incompatible types issue when compiling - void to MimeMessage

58 Views Asked by At

I have encountered an issue while compiling my app, the error is: incompatible types: void cannot be converted to javax.mail.internet.MimeMessage

It occurs in this part of the code:

getEmailService().parseEmail(parseEmailRequest.getFileName(), parseEmailRequest.getFileContent());

and the methods are as follows:

private EmailService getEmailService() {
return new EmailService();
}

and

public MimeMessage parseEmail(String fileName, Part fileContent) {
        return getParseEmailUseCase()
                .withEmailFileStorage(getEmailFileStorage())
                .withFilePathBuilder(getFilePathBuilder())
                .withEmailParser(getEmailParser())
                .withFileName(fileName)
                .withFileContent(fileContent)
                .run();
    }

This is strange to be because I wanted it as a variable message

private void setEmailMessage(ParseEmailRequest parseEmailRequest) {
    message = getEmailService()
            .parseEmail(parseEmailRequest.getFileName(), parseEmailRequest.getFileContent());
}

message is a field

private MimeMessage message;

and this is when I get error and when I remove message and keep only:

getEmailService().parseEmail(parseEmailRequest.getFileName(), parseEmailRequest.getFileContent());

program is compiled. What is more, when I use my IDE (intellij) for assigning a variable to above code, MimeMessage is automatically selected so it means getEmailService().parseEmail(parseEmailRequest.getFileName(), parseEmailRequest.getFileContent()); returns MimeMessage as I wanted.

All of the above is happning in class EmailHandler.

I don't understand where the void comes from in the error message since my methods return specified types. What am I missing here?

1

There are 1 best solutions below

5
On

getEmailService().(parseEmailRequest.getFileName(), parseEmailRequest.getFileContent());

should be:

getEmailService().parseEmail(parseEmailRequest.getFileName(), parseEmailRequest.getFileContent());

You didn't provide much context to your code, but from what I assume, you intend to create the message object within EmailService. getEmailService() returns you an instance to the object EmailService which then lets you call the parseEmail() method. Note that if EmailService is intended as a factory class of some sort, other classes won't be able to use its methods if they are using the publicity modifier private.