Error: Attempted to call method "format" on null context object

13,765

Solution 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>

Solution 2

If you use springboot and configuration as code

add : templateEngine.addDialect(new Java8TimeDialect());

Share:
13,765
adam.shaleen
Author by

adam.shaleen

Stumbled into development later in life after a long bout in the sales and service field. Trying to learn a little bit every day and find my place in this industry.

Updated on July 18, 2022

Comments

  • adam.shaleen
    adam.shaleen almost 2 years

    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?

  • Igor Delac
    Igor Delac about 3 years
    useful to know this