Issue
- Adding a new field in an existing estructure produces an
InvalidReferenceExceptionin articles unless you update them if you try to render this field in a template:freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134)
at freemarker.core.UnexpectedTypeException.newDescriptionBuilder(UnexpectedTypeException.java:85)
at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:48)
at freemarker.core.NonHashException.<init>(NonHashException.java:49)
at freemarker.core.Dot._eval(Dot.java:48)
at freemarker.core.Expression.eval(Expression.java:101)
at freemarker.core.MethodCall._eval(MethodCall.java:55)
at freemarker.core.Expression.eval(Expression.java:101)
at freemarker.core.Assignment.accept(Assignment.java:134)
at freemarker.core.Environment.visit(Environment.java:331)
at freemarker.core.Environment.visit(Environment.java:337)
at freemarker.core.Environment.visit(Environment.java:337)
at freemarker.core.Environment.process(Environment.java:310)
at freemarker.template.Template.process(Template.java:383)
at com.liferay.portal.template.freemarker.internal.FreeMarkerTemplate.processTemplate(FreeMarkerTemplate.java:154)
at com.liferay.portal.template.BaseTemplate.processTemplate(BaseTemplate.java:163)
at com.liferay.journal.internal.transformer.JournalTransformer.doTransform(JournalTransformer.java:367)
at com.liferay.journal.internal.transformer.JournalTransformer.transform(JournalTransformer.java:103)
Environment
- Liferay DXP 7.0
- Liferay DXP 7.1
- Liferay DXP 7.2
- Liferay DXP 7.3
- Liferay DXP 7.4
Resolution
- The problem here is that the structure has a field but the article content does not, so if you try to render the structure the field will be null and it will throw a null pointer exception when the
getData()method is used. - This NPE will be shown as the
InvalidReferenceExceptionby the template engine. - The solution is to add a null check in the template for this kind of fields to avoid rendering them if they are
null. - Instead of rendering it like
<#assign field = field.getData()>
you could use<#if field?? && field.getData()?? > <#assign field = field.getData()>
<#else>
<#assign field = "">
</#if>