Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package freemarker.core;
A subclass of freemarker.template.TemplateException that says that an FTL expression has evaluated to null or it refers to something that doesn't exist. At least in FreeMarker 2.3.x these two cases aren't distinguished.
 public class InvalidReferenceException extends TemplateException {
     static final InvalidReferenceException FAST_INSTANCE;
     static {
         Environment prevEnv = Environment.getCurrentEnvironment();
         try {
              = new InvalidReferenceException(
                     "Invalid reference. Details are unavilable, as this should have been handled by an FTL construct. "
                     + "If it wasn't, that's problably a bug in FreeMarker.",
         } finally {
     private static final String[] TIP = new String[] {
         "If the failing expression is known to be legally refer to something that's sometimes null or missing, "
         + "either specify a default value like myOptionalVar!myDefault, or use ",
         "<#if myOptionalVar??>""when-present""<#else>""when-missing""</#if>",
         ". (These only cover the last step of the expression; to cover the whole expression, "
         + "use parenthesis: (!myDefault, ("
     private static final String[] TIP_MISSING_ASSIGNMENT_TARGET = {
             "If the target variable is known to be legally null or missing sometimes, instead of something like ",
             "<#assign x += 1>"", you could write ""<#if x??>""<#assign x += 1>""</#if>",
             " or ""<#assign x = (x!0) + 1>"
     private static final String TIP_NO_DOLLAR =
             "Variable references must not start with \"$\", unless the \"$\" is really part of the variable name.";
     private static final String TIP_LAST_STEP_DOT =
             "It's the step after the last dot that caused this error, not those before it.";
     private static final String TIP_LAST_STEP_SQUARE_BRACKET =
             "It's the final [] step that caused this error, not those before it.";
     private static final String TIP_JSP_TAGLIBS =
             "The \"JspTaglibs\" variable isn't a core FreeMarker feature; "
             + "it's only available when templates are invoked through freemarker.ext.servlet.FreemarkerServlet"
             + " (or other custom FreeMarker-JSP integration solution).";
Creates and invalid reference exception that contains no information about what was missing or null. As such, try to avoid this constructor.
     public InvalidReferenceException(Environment env) {
         super("Invalid reference: The expression has evaluated to null or refers to something that doesn't exist.",

Creates and invalid reference exception that contains no programmatically extractable information about the blamed expression. As such, try to avoid this constructor, unless need to raise this expression from outside the FreeMarker core.
     public InvalidReferenceException(String descriptionEnvironment env) {

This is the recommended constructor, but it's only used internally, and has no backward compatibility guarantees.

expression The expression that evaluates to missing or null. The last step of the expression should be the failing one, like in goodStep.failingStep.furtherStep it should only contain goodStep.failingStep.
     InvalidReferenceException(_ErrorDescriptionBuilder descriptionEnvironment envExpression expression) {

Use this whenever possible, as it returns FAST_INSTANCE instead of creating a new instance, when appropriate.
        if (env != null && env.getFastInvalidReferenceExceptions()) {
            return ;
        } else {
            if (blamed != null) {
                final _ErrorDescriptionBuilder errDescBuilder
                        = new _ErrorDescriptionBuilder("The following has evaluated to null or missing:").blame(blamed);
                if (endsWithDollarVariable(blamed)) {
           Object[] {  });
                } else if (blamed instanceof Dot) {
                    final String rho = ((Dotblamed).getRHO();
                    String nameFixTip = null;
                    if ("size".equals(rho)) {
                        nameFixTip = "To query the size of a collection or map use ?size, like myList?size";
                    } else if ("length".equals(rho)) {
                        nameFixTip = "To query the length of a string use ?length, like myString?size";
                            nameFixTip == null
                                    ? new Object[] {  }
                                    : new Object[] { nameFixTip });
                } else if (blamed instanceof DynamicKeyName) {
           Object[] {  });
                } else if (blamed instanceof Identifier
                        && ((Identifierblamed).getName().equals("JspTaglibs")) {
           Object[] {  });
                } else {
                return new InvalidReferenceException(errDescBuilderenvblamed);
            } else {
                return new InvalidReferenceException(env);
Used for assignments that use operators like +=, when the target variable was null/missing.
    static InvalidReferenceException getInstance(String missingAssignedVarNameString assignmentOperator,
            Environment env) {
        if (env != null && env.getFastInvalidReferenceExceptions()) {
            return ;
        } else {
            final _ErrorDescriptionBuilder errDescBuilder = new _ErrorDescriptionBuilder(new Object[] {
                            "The target variable of the assignment, ",
                            new _DelayedJQuote(missingAssignedVarName),
                            ", was null or missing, but the \"",
                            assignmentOperator"\" operator needs to get its value before assigning to it."
            if (missingAssignedVarName.startsWith("$")) {
       Object[] {  });
            } else {
            return new InvalidReferenceException(errDescBuilderenvnull);
    private static boolean endsWithDollarVariable(Expression blame) {
        return blame instanceof Identifier && ((Identifierblame).getName().startsWith("$")
                || blame instanceof Dot && ((Dotblame).getRHO().startsWith("$");
New to GrepCode? Check out our FAQ X