antlr/antlr4 · error · IllegalArgumentException

tree cannot be null

Error message

tree cannot be null

What it means

Error "tree cannot be null" thrown in antlr/antlr4.

Source

Thrown at runtime/Java/src/org/antlr/v4/runtime/tree/pattern/ParseTreeMatch.java:56

	/**
	 * Constructs a new instance of {@link ParseTreeMatch} from the specified
	 * parse tree and pattern.
	 *
	 * @param tree The parse tree to match against the pattern.
	 * @param pattern The parse tree pattern.
	 * @param labels A mapping from label names to collections of
	 * {@link ParseTree} objects located by the tree pattern matching process.
	 * @param mismatchedNode The first node which failed to match the tree
	 * pattern during the matching process.
	 *
	 * @exception IllegalArgumentException if {@code tree} is {@code null}
	 * @exception IllegalArgumentException if {@code pattern} is {@code null}
	 * @exception IllegalArgumentException if {@code labels} is {@code null}
	 */
	public ParseTreeMatch(ParseTree tree, ParseTreePattern pattern, MultiMap<String, ParseTree> labels, ParseTree mismatchedNode) {
		if (tree == null) {
			throw new IllegalArgumentException("tree cannot be null");
		}

		if (pattern == null) {
			throw new IllegalArgumentException("pattern cannot be null");
		}

		if (labels == null) {
			throw new IllegalArgumentException("labels cannot be null");
		}

		this.tree = tree;
		this.pattern = pattern;
		this.labels = labels;
		this.mismatchedNode = mismatchedNode;
	}

	/**
	 * Get the last node associated with a specific {@code label}.

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Pass a non-null ParseTree to the ParseTreeMatch constructor; null-check the tree at the call site before constructing.
  2. If the tree comes from parsing, verify parser.getNumberOfSyntaxErrors() == 0 and the parse result is not null before matching.

Example fix

ParseTree tree = parser.startRule();
if (tree == null) throw new IllegalArgumentException("parse failed");
ParseTreeMatch match = pattern.match(tree);

When it happens

Trigger: ParseTreeMatch constructed with a null tree argument.

Common situations: Always pass a non-null ParseTree to the ParseTreeMatch constructor; validate parser result (e.g. parser.rule()) is not null before matching.


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/e19823521ed7d839. Report an issue: GitHub.