Issue since migrating to version 3.0.6 / spring 5: one variable in context is null

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Issue since migrating to version 3.0.6 / spring 5: one variable in context is null

balteo
Hello,
I get the following exception (see below) since migrating to spring 5 / Thymeleaf 3.0.

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'text' cannot be found on null
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:205) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:98) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:50) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:362) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:106) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:271) ~[spring-expression-5.0.0.RC2.jar:5.0.0.RC2]
        at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.6.M4.jar:3.0.6.M4]
        ... 160 common frames omitted

The code that prepares and populate the context with variables is as follows:

    private MimeMessage prepareMessagesDigest(UserAccount userAccount) {
        try {
            final Context ctx = new Context();
            ctx.setVariable("userAccount", userAccount);
            ctx.setVariable("websiteContext", websiteContext);
            final MimeMessage mimeMessage = mailSender.createMimeMessage();
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, MULTIPART, ENCODING);
            message.setSubject("Message digest");
            message.setFrom(noReplyEmailAddress);
            message.setTo(userAccount.getEmail());
            final String htmlContent = templateEngine.process("messagesDigest", ctx);
            message.setText(htmlContent, IS_HTML);
            return mimeMessage;
        } catch (MessagingException e) {
            throw new MailPreparationException("MailPreparationException", e);
        }
    }

Here is my template:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<p th:text="${userAccount.email}"></p>
	<p th:each="message: ${userAccount.receivedMessages}">
		<div th:text="${message.text}"></div>
		from: <span th:text="${message.sender.firstName}"></span>
	</p>
	<p>
		 <a th:href="@{${websiteContext} + '/#/dashboard'}">Consulter ma messagerie</a>
	</p>
</body>
</html>

Somehow, the message variable is null here: ${message.text}

However, when I debug the code, I do see that the receivedMessages variable does contain messages.

Can someone please help?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Issue since migrating to version 3.0.6 / spring 5: one variable in context is null

balteo
It is very odd. This issue started appearing after I migrated to spring 5 and the corresponding thymeleaf version.

This issue seems to be related to the
<p>
 tag.

If I replace the
<p>
 tag with a
<div>
 tag, the issue is fixed...

See below for the fix:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<p th:text="${userAccount.email}"></p>
	<div th:each="message: ${userAccount.receivedMessages}">
		<div th:text="${message.text}"></div>
		from: <span th:text="${message.sender.firstName}"></span>
	</div>
	<p>
		 <a th:href="@{${websiteContext} + '/#/dashboard'}">Consulter ma messagerie</a>
	</p>
</body>
</html>

Is this expected behavior?
Loading...