parsing a JSON string - null pointer exception

58 views
Skip to first unread message

Peter Wallis

unread,
Oct 1, 2021, 7:36:55 AM10/1/21
to mozilla-rhino
This seems crazy - I used to have it working, and it seems trivial but this:

import org.mozilla.javascript.*;
import java.io.*;

public class JTest {

        public static void main(String[] args) {
                Context cx = Context.enter();
                Scriptable scope = cx.initStandardObjects();
                try{
                        NativeJSON.parse(cx,scope,"{\"foo\":\"bar\"}",null);
                } catch(Exception ex) {
                        ex.printStackTrace();
                } finally {
                        Context.exit();
                }

        }
}

Gives a null pointer exception in NativeJSON.walk (line 177 of the java file)
It seems so trivial that I am guessing it is me rather than a bug.  Any help appreciated.
P

Tony Germano

unread,
Oct 5, 2021, 8:30:22 PM10/5/21
to mozilla-rhino
It looks like you want to call the 3 argument version of parse that doesn't take a reviver, but it is private (I'm not sure why.)

To call the 4 argument version, it looks like you will need to pass a Callable reviver instead of null.

What if you add your JSON string to the scope, and do this?
cx.evaluateString(scope, "JSON.parse(jsonString);", "parse.js", 1, null);
Reply all
Reply to author
Forward
0 new messages