Error: Attempted to call method “format” on null context object

浪子不回头ぞ 提交于 2019-12-22 05:47:08

问题


Spring-boot v1.4.1
Java v1.8
Thymeleaf v2.1.5.

The following line of code in my view:

<td th:each = "sprint : ${sprints}" th:text = "${sprint.releaseDate} ? ${#temporals.format(sprint.releaseDate, 'MMM/dd/yyyy')}"></td>

which has syntax I am basing off of the S.O. question SpringBoot Thymeleaf Ordinal Numbers, produces the error:

org.springframework.expression.spel.SpelEvaluationException: EL1011E:(pos 11): Method call: Attempted to call method format(java.time.LocalDate,java.lang.String) on null context object

However, if I run this line of code without the Thymeleaf formatting it works and renders a table of LocalDate objects in default format ("2016-05-25").

Question: Why am I getting a 'null context object' error and what does that mean? And how can I edit to get the formatting I want?


回答1:


To use #temporals object you need include thymeleaf-extras-java8time module to your project. Here is GitHub page of the extras module.

This module adds a #temporals object similar to the #dates or #calendars ones in the Standard Dialect, allowing the formatting and creation of temporal objects from Thymeleaf templates.

In version 1.4.1 of Spring Boot it is only necessary to include the extras module, and autoconfiguration will set up it for you. Make sure that you provided the proper version, depends on your Thymeleaf version:

  • Version 3.0.0.RELEASE - for Thymeleaf 3.0 (requires Thymeleaf 3.0.0+)
  • Version 2.1.0.RELEASE - for Thymeleaf 2.1 (requires Thymeleaf 2.1.3+)

I've got the same spring boot and thymeleaf versions as you and have received the same error just because I provide inappropriate version of extras (3.0.0). Switching it to lower version fixed the problem (in my case in maven pom file):

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>


来源:https://stackoverflow.com/questions/40157364/error-attempted-to-call-method-format-on-null-context-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!