You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring+Thymeleaf调用空对象方法报错EL1011E求助

问题排查与解决

报错信息

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "instrucor.getInstructorId()" (template: "views/update" - line 39, col 14)
    at org.thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292) ~[thymeleaf-spring6-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.1.0.RELEASE.jar:3.1.0.RELEASE]
6-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    ... 64 common frames omitted

2023-01-06T09:29:51.435+02:00 ERROR 18336 --- [nio-8071-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "instrucor.getInstructorId()" (template: "views/update" - line 39, col 14)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getInstructorId() on null context object
    at org.springframework.expression.spel.ast.MethodReference.throwIfNotNullSafe(MethodReference.java:154) ~[spring-expression-6.0.2.jar:6.0.2]

环境版本

  • Spring版本:3.0.0
  • Thymeleaf版本:3.1

相关代码

HTML+Thymeleaf代码

<div class="mb-3 mt-3"> 
   <label class ="form-label"> Instructor: </label>
   <select th:field="${course.instructor}" name="instructor" class="form-select"> 
    
    <option th:each="instructor:${listInstructor}" th:text="${instructor.getfirstName()} + ' ' +${instructor.getlastName()}"
             th:value="${instrucor.getInstructorId()}"> </option>
   </select>
   </div> 

Controller代码

@GetMapping ("/update")
public String updateCourse (Model model, Long courseId) {
    Course course = courseService.loadCourseBycourseId(courseId);
    List <Instructor> instructors = instructorService.fetchInstrucotr();
    model.addAttribute("course", course);
    model.addAttribute("listInstructor", instructors);
    return "views/update";
}

@PostMapping ("/save")
public String saveCourse (Course course) {
    courseService.createOrUpdateCourse(course);
    return "redirect:/courses/index";
}

现象说明

MySQL数据库中存在Instructor数据;移除HTML中的讲师字段后,页面可正常加载。


原因分析

  1. 变量拼写错误:Thymeleaf代码里th:value="${instrucor.getInstructorId()}"的instrucor是拼写错误,正确变量名应为循环中的instructor,拼写错误导致SpringEL找不到对应变量,视为null,调用方法时抛出异常。
  2. 方法名拼写错误:Controller中fetchInstrucotr()少了一个字母r,正确应为fetchInstructor(),虽然当前页面移除字段后能加载,但该错误可能导致讲师列表无法正确获取。
  3. getter方法调用不规范:Thymeleaf中getfirstName()、getlastName()不符合Java驼峰命名,正确应为getFirstName()、getLastName(),可能导致无法正确调用实体类的getter方法。

解决方法

1. 修正Thymeleaf代码

修改<option>标签,修正拼写和方法调用,也可以直接使用属性访问(SpringEL支持直接访问实体属性,无需显式调用getter):

<option th:each="instructor:${listInstructor}" 
        th:text="${instructor.firstName} + ' ' + ${instructor.lastName}"
        th:value="${instructor.instructorId}"> 
</option>

2. 修正Controller方法名

修改Controller中的服务调用代码:

List <Instructor> instructors = instructorService.fetchInstructor();

3. 验证数据有效性

确认instructorService.fetchInstructor()能从数据库查询到非空的讲师列表,确保循环有数据可遍历。


内容的提问来源于stack exchange,提问作者user18583088

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:10:15