Menu

#257 ParseException.getErrorLineNumber gives NullPointerException

None
closed
nobody
Parsing (77)
5
2018-08-02
2006-03-22
No

Under some situations, ParseException is constructed
with a null "currentToken" member variable.

A call to ParseException.getErrorLineNumber therefore
fails with a NullPointerException.

The exception message has however been correctly
populated:

Parse error at line 35, column 20. Encountered: (

The source code line that caused this problem is:

response.setContent(server.processTemplate("_search.html",templateVars,"text/html");

That is, there is a missing ")".

Discussion

  • Moritz Ringler

    Moritz Ringler - 2006-11-23

    Logged In: YES
    user_id=730046
    Originator: NO

    I also encountered this bug. The bsh code that raised the error was
    Number num = (Number a;
    i. e. also a missing ')' [between "(Number" and " a"]

    The immediate reason for the problem is that all but the 'specialConstructor' constructors do not fill in the token information. Therefore, getErrorLineNumber should be
    return (specialConstructor)? currentToken.next.beginLine : super.getErrorLineNumber();

    The less immediate reason is that the generateParseException() method in the Parser class uses the ParseException(String) constructor and not the "special" constructor ParseException(Token, int[][], String[]), and therefore the currentToken is not set even though it could be.
    To resolve this we could replace line 5807 in Parser with:
    ParseException ex = new ParseException("Parse error at line " + line + ", column " + column + ". Encountered: " + mess);
    ex.currentToken = token;

    and use
    return (currentToken != null && currentToken.next != null) currentToken.next.beginLine : super.getErrorLineNumber();

    for the getErrorLineNumber() method in ParseException.

     
  • Moritz Ringler

    Moritz Ringler - 2006-11-23

    Logged In: YES
    user_id=730046
    Originator: NO

    The code in my last comment was not 100% correct so here is the diff output for something that will work.

    Parser java:
    5807c5807,5809
    < return new ParseException("Parse error at line " + line + ", column " + column + ". Encountered: " + mess);
    ---
    > ParseException ex = new ParseException("Parse error at line " + line + ", column " + column + ". Encountered: " + mess);
    > ex.currentToken = token;
    > return ex;

    ParseException.java:
    1a2
    >
    270c271,272
    < return currentToken.next.beginLine;
    ---
    > return (currentToken != null && currentToken.next != null)?
    > currentToken.next.beginLine : super.getErrorLineNumber();

     
  • nickl-

    nickl- - 2018-08-02
    • status: open --> closed
    • Group: -->
     
  • nickl-

    nickl- - 2018-08-02

    Ticket has been migrated to github.
    Please follow up on this over here: https://github.com/beanshell/beanshell/issues/395

     

Log in to post a comment.