Java拼接字符串报错Operator '+' cannot be applied to 'String','void'
问题描述
使用forEach遍历对象列表拼接字符串时,触发编译错误:Operator '+' cannot be applied to 'java.lang.String', 'void',无法定位问题原因。
报错出现在sendMailOwner方法的邮件正文拼接逻辑中,预期通过PaidRequest.getDatesRequest().forEach((p)->p.getStartDate)的写法,从PaidRequest的List<DatesRequest>类型属性datesRequest中取出所有元素的startDate、endDate字段,拼接到待发送的邮件正文中。
关联代码
异常方法代码
public void sendMailOwner(VacacionRequest PaidRequest) { CollaboratorDTO validator = OrganizationalUintService.findValidator(collaboratorTransformer.entityTranferToDTO(PaidRequest.getCollaborator())); EmailService.sendSimpleMessage(PaidRequest.getCollaborator().getEmail(), "EverHoliday", " Bonjour "+PaidRequest.getCollaborator().getFirstname()+" "+PaidRequest.getCollaborator().getLastname()+"," + "\n Votre demande de Congé payé du date "+PaidRequest.getDatesRequest().forEach((p)->p.getStartDate)+"au"+PaidRequest.getDatesRequest().forEach((p)->p.getEndDate)+" est en attente de validation par : " +validator.getLastname()+" "+validator.getFirstname() + " \n Cordialement."); }
代码中
PaidRequest.getDatesRequest().forEach((p)->p.getStartDate)的预期作用是获取datesRequest列表内所有元素的startDate值
DatesRequest模型定义
package com.example.demo.model; import java.time.LocalDate; import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Node; @Node public class DatesRequest { @Id @GeneratedValue private Long id; private LocalDate startDate; private LocalDate endDate; private double duration; public DatesRequest(LocalDate startDate, LocalDate endDate, double duration) { this.startDate = startDate; this.endDate = endDate; this.duration = duration; } public DatesRequest() { } public LocalDate getStartDate() { return startDate; } public void setStartDate(LocalDate startDate) { this.startDate = startDate; } public LocalDate getEndDate() { return endDate; } public void setEndDate(LocalDate endDate) { this.endDate = endDate; } public double getDuration() { return duration; } public void setDuration(double duration) { this.duration = duration; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
PaidRequest关联构造函数
public PaidRequest(LocalDate requestDate, String statut, String typeOfTime, List<DatesRequest> datesRequest, Collaborator collaborator, String description, double balanceUsed,String justification) { super(requestDate, statut, typeOfTime, datesRequest, collaborator); this.description = description; this.balanceUsed = balanceUsed; this.justification = justification; }
根因分析
- Java中
List.forEach()方法的返回值类型为void,不返回任何执行结果,无法直接参与字符串+拼接操作,这是编译报错的直接原因:编译器检测到你试图将无返回值的forEach执行结果和String类型值做加法运算,类型不匹配因此抛出对应错误。 - 代码中写的lambda表达式
(p)->p.getStartDate仅在遍历过程中读取了每个元素的startDate值,既没有对取值做存储,也没有做拼接聚合,完全无法实现“提取列表所有日期值拼接成字符串”的预期效果。
修复方案
不要将forEach直接写入字符串拼接逻辑,根据业务场景选择对应实现:
- 如果
datesRequest可能包含多个时间段(比如跨段请假),使用Stream流做字段提取和字符串聚合:// 注意类顶部需要导入 java.util.stream.Collectors 包 public void sendMailOwner(VacacionRequest PaidRequest) { CollaboratorDTO validator = OrganizationalUintService.findValidator(collaboratorTransformer.entityTranferToDTO(PaidRequest.getCollaborator())); // 提前聚合拼接所有开始、结束日期,多个日期用逗号分隔 String startDates = PaidRequest.getDatesRequest().stream() .map(DatesRequest::getStartDate) .map(LocalDate::toString) .collect(Collectors.joining(", ")); String endDates = PaidRequest.getDatesRequest().stream() .map(DatesRequest::getEndDate) .map(LocalDate::toString) .collect(Collectors.joining(", ")); EmailService.sendSimpleMessage(PaidRequest.getCollaborator().getEmail(), "EverHoliday", " Bonjour "+PaidRequest.getCollaborator().getFirstname()+" "+PaidRequest.getCollaborator().getLastname()+"," + "\n Votre demande de Congé payé du date "+ startDates +" au "+ endDates +" est en attente de validation par : " +validator.getLastname()+" "+validator.getFirstname() + " \n Cordialement."); } - 如果业务上单次请假仅对应一个时间段(
datesRequest列表永远只有1个元素),直接取首个元素的字段即可,无需遍历:DatesRequest requestDate = PaidRequest.getDatesRequest().get(0); // 字符串拼接时直接使用 requestDate.getStartDate()、requestDate.getEndDate()
内容的提问来源于stack exchange,提问作者Nouhaila Khaouti
相关产品推荐
相关产品推荐

