0

I try to evaluate math expression in string form using scriptengine, but it throws me NullPointerException

java.lang.NullPointerException
    at Math.main(Math.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Here is the code:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Math{
  public static void main(String[] args){
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
    try {
      System.out.println("scriptEngine result: "+jsEngine.eval("{2+4*3*[1+(1+2+4)]}"));
    } catch (ScriptException ex) {
      Logger.getLogger(Math.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

i tried the same code in netbeans and it works, but i also need it to work in DrJava development environment. Is it possible to resolve this or is there any other library that can evaluate string like this with combination of all parenthesis "{[()]}"?

alonso05
  • 129
  • 3
  • 15
  • Assuming line 12(NPE) as the line which contains jsEngine.eval, you can debug and check if jsEngine is null. I see a compiler error (may be during compiling javascript) in you stack trace, by any chance are you using different JDK in your drjava classpath and not the one which you used in netbeans? – Arun Chandrasekaran Aug 27 '16 at 19:06
  • i use the same JDK, can you suggest some other library for evaluate math expression with all types of parenthesis – alonso05 Sep 05 '16 at 09:36

0 Answers0