antlr/antlr4 · error · ArgumentNullException

listener

Error message

listener

What it means

Error "listener" thrown in antlr/antlr4.

Source

Thrown at runtime/CSharp/src/Parser.cs:454

        /// behavior of the listener calls.</li>
        /// <li>Alterations to the command line options passed to ANTLR 4 when
        /// generating the parser may change the behavior of the listener calls.</li>
        /// <li>Changing the version of the ANTLR Tool used to generate the parser
        /// may change the behavior of the listener calls.</li>
        /// </ul>
        /// </summary>
        /// <param name="listener">the listener to add</param>
        /// <exception cref="System.ArgumentNullException">
        /// if
        /// <c/>
        /// listener is
        /// <see langword="null"/>
        /// </exception>
        public virtual void AddParseListener(IParseTreeListener listener)
        {
            if (listener == null)
            {
                throw new ArgumentNullException("listener");
            }
            if (_parseListeners == null)
            {
                _parseListeners = new List<IParseTreeListener>();
            }
            this._parseListeners.Add(listener);
        }

        /// <summary>
        /// Remove
        /// <paramref name="listener"/>
        /// from the list of parse listeners.
        /// <p>If
        /// <paramref name="listener"/>
        /// is
        /// <see langword="null"/>
        /// or has not been added as a parse
        /// listener, this method does nothing.</p>

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Do not pass a null listener to AddParseListener/RemoveParseListener; check for null before calling.
  2. If removing, pass the same listener instance previously added.

Example fix

var listener = new MyListener();
parser.AddParseListener(listener);
// later
parser.RemoveParseListener(listener);

When it happens

Trigger: Parser.RemoveErrorListener(null) or similar call passes a null listener argument.

Common situations: Only add/remove non-null IAntlrErrorListener instances; keep references to listeners added so removal passes the same instance.


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