[antlr-interest] Java Tree Parser: NPE: rulememo == null??
Randall R Schulz
rschulz at sonic.net
Fri Jul 13 19:39:30 PDT 2007
On Friday 13 July 2007 16:23, Randall R Schulz wrote:
> Hi,
>
> After a recent round of changes to my tree grammar, most of them in
> parser class methods and other related classes, not the grammar
> itself, the resulting parser is now crashing with an NPE in
> (org.antlr.runtime) BaseRecognizer.memoize(IntStream, int, int). This
> line of code is:
>
> if ( ruleMemo[ruleIndex]!=null ) { ... }
>
> Exception in thread "main" java.lang.NullPointerException
> at
> org.antlr.runtime.BaseRecognizer.memoize(BaseRecognizer.java:740)
>
> ... [ other examples of NPE upon access to ruleMemo ] ...
So here's what happend. I added a new constructor to my tree parser that
takes other parameters (obviously). Here's the problem. The constructor
supplied by ANTLR looks like this:
public CLIFBuilder(TreeNodeStream input) {
super(input);
ruleMemo = new HashMap[57+1];
}
Naturally, my constructors don't contain the line that allocates the
array of HashMaps for ruleMemo.
Is it expressly forbidden to override the constructor of an
ANTLR-generated parser class? If not, then there needs to be a way to
handle the need to allocate the ruleMemo array.
At a minimum, there should be some documentation that expresses this
requirement. If that documentation exists, I couldn't find it.
The solution is simple enough: Where overloaded parser constructors
might (as mine did) call "super(input)" they should instead
call "this(input)".
Randall Schulz
More information about the antlr-interest
mailing list