Search code examples
javaintellij-ideanashorn

Null pointer exception while trying to access js file using Java Nashorn


I'm trying to make an IDE for HolyC using Java. I already have worked on a simple text editor using Java so I used it as a base.

Apparently the interpreter that I'm planning to use for this IDE is written in JavaScript so I've tried loading the .js file by placing it in the same src folder as my .java. code.

I used Nashorn for this since I found it to be the most suitable engine for my project but the issue is that the code throws a null pointer exception when I run the code.

This is the error

"C:\Program Files\Java\jdk-21\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.5\lib\idea_rt.jar=60192:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.5\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath "C:\Users\aryan\OneDrive\Documents\Github\prometheus-ide\Prometheus IDE\out\production\Prometheus IDE" interpreter
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "javax.script.ScriptEngine.eval(java.io.Reader)" because "ee" is null
    at interpreter.main(interpreter.java:8)

Process finished with exit code 1

and this is my code

//importing modules
import javax.script.*;
import java.io.*;
public class interpreter {
    public static void main(String[] args) throws Exception{
        //calling nashorn js engine
        ScriptEngine ee = new ScriptEngineManager().getEngineByName("Nashorn");
        ee.eval(new FileReader("C:/Users/aryan/OneDrive/Documents/Github/prometheus-ide/src/holyc-interpreter.js"));
    }
}

I can't seem to figure out what's wrong because this is my first null pointer exception in Java


Solution

  • Nashorn has been deprecated in JDK 15 so it no longer exists in the JDK and throws a null pointer exception as told by @duffymo.

    Rhino is an alternative suggested by @g00se