0

i have this code:

uguale.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String expression = "12+17";
        String result;
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        try {
            result = engine.eval(expression).toString();
            ResBar.setText(result);
        } catch (Exception e) {
            ResBar.setText("error");
        }
    }
});

in my app always appears error, and never comes into try but always comes into catch, why?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 1
    what exception do you get? show us log – Ahad Feb 17 '17 at 14:07
  • 1
    Use `e.printStackTrace()` to get the log of the error – zed Feb 17 '17 at 14:08
  • java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object javax.script.ScriptEngine.eval(java.lang.String)' on a null object reference – register_pippo Feb 17 '17 at 14:19
  • @register_pippo "engine" variable is null , possibile duplicate , see this question http://stackoverflow.com/questions/30007787/why-does-getenginebyname-js-return-null – an_droid_dev Feb 17 '17 at 14:31
  • @an_droid_dev I could not find the answer – register_pippo Feb 17 '17 at 14:58
  • Since there is no `ScriptEngineManager` in the Android SDK, your problem presumably lies there. Talk to whoever supplied you with that class and ask them how to get a `"JavaScript"` script engine. – CommonsWare Feb 17 '17 at 15:28
  • i included import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; import javax.script.ScriptException; – register_pippo Feb 17 '17 at 15:59

0 Answers0