【问题标题】:Method call: Attempted to call method on null context object方法调用:尝试在空上下文对象上调用方法
【发布时间】:2023-01-16 19:33:19
【问题描述】:

当我加载我的应用程序页面时,我给出下一个异常

 org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "instructor.user.getUserEmail()" (template: "views/instructorCreate" - line 84, col 6)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getUserEmail() on null context object
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "instructor.user.getUserEmail()" (template: "views/instructorCreate" - line 84, col 6)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getUserEmail() on null context object

我的版本 Thymeleaf 3.1 和 Spring 6.0.2

我的 Html + Thymeleaf 页面

<div class="mb-3 mt-3">
                <label for="userEmail" class="form-label"> Email :</label>
                <input id="userEmail" type="email" name="userEmail" class="form-control"
                    th:value="${instructor.user.getUserEmail()}" required>
                    <span th:errors="${instructor.user.email}"> </span>
            </div>
            
            <div class="mb-3 mt-3">
                <label for="userPassword" class="form-label"> Password :</label>
                <input id="userPassword" type="text" name="userPassword" class="form-control"
                    th:value="${instructor.user.getUserPassword()}" required>
            </div>

用户获取器

public String getUserEmail() {
        return userEmail;
    }

教师用户领域;

@OneToOne (cascade = CascadeType.REMOVE)
    @JoinColumn (name = "user_id", referencedColumnName = "user_id", nullable = false)
    private User user;

如果你知道我的问题在哪里,我会感谢你

【问题讨论】:

  • 好吧,显然 instructor.user 是空的。现在您需要找出原因:它是否存在于数据库中?您的 JPA 注释不正确吗?您如何创建 instructor 的实例?
  • 是的,在数据​​库中存在,JPA 正确,在控制器中创建实例。

标签: spring-boot hibernate thymeleaf


【解决方案1】:

我的控制器

@GetMapping ("/formCreate")
    public String createInstructor(Model model) {
        model.addAttribute("instructor", new Instructor());
        return "views/instructorCreate";
    
    }

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 2021-09-03
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    相关资源
    最近更新 更多