ScriptEngineManager
Author |
Message |
andrew.
|
Posted: Sun May 31, 2009 11:25 am Post subject: ScriptEngineManager |
|
|
Hey guys,
For our final ISU in CS, we must make a calculator in Java. I already had everything set up until the teacher told us about using JavaScript to evaluate our expressions. Right now I'm using this method to evaluate an expression in the form of a string.
Java: | public String calculate(String inputexp)
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try
{
Object result = engine.eval(inputexp);
System.out.println(inputexp+" = "+result);
return result.toString();
}
catch(ScriptException se)
{
return "Math error: "+se.toString();
}
} |
According to the websites I've been reading this should be right. But when I compile and I pass a string to this method, I get this error message immediately (before it even gets to the System.out.println line):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Calculator.MainWin.calculate(SuperCalcAndrewHassan.java:225)
at Calculator.MainWin.actionPerformed(SuperCalcAndrewHassan.java:199)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6126)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5891)
at java.awt.Container.processEvent(Container.java:2102)
at java.awt.Component.dispatchEventImpl(Component.java:4497)
at java.awt.Container.dispatchEventImpl(Container.java:2160)
at java.awt.Component.dispatchEvent(Component.java:4327)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4366)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4030)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3960)
at java.awt.Container.dispatchEventImpl(Container.java:2146)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4327)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:300)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:210)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:195)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:187)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Anyone know why this would happen? Also, I am using JVM 1.6.0. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Sun May 31, 2009 1:30 pm Post subject: RE:ScriptEngineManager |
|
|
Check the values of the manager and engine variables before using them (that is, after retrieving each, try outputting their values). It looks like one or both is coming back as null.
Alternately, notice that it says it blew up on line 225 of your SuperCalcAndrewHassan.java file and look at what pointers that line uses. |
|
|
|
|
|
andrew.
|
Posted: Mon Jun 01, 2009 5:56 pm Post subject: RE:ScriptEngineManager |
|
|
Line 255 is this:
code: | Object result = engine.eval(inputexp); |
I think it may be my installation of JDK because when I run the command:
code: | List <ScriptEngineFactory> factories = mgr.getEngineFactories(); |
There is only an AppleScript entry which means that Javascript isn't even there for use.
Edit: I am going to compile and run it on a different machine.
Edit 2: Yeah, that's why it wasn't working. Thanks for your help though. |
|
|
|
|
|
DemonWasp
|
Posted: Mon Jun 01, 2009 7:43 pm Post subject: RE:ScriptEngineManager |
|
|
Ah, well that's what I was saying - if you only have an AppleScript engine available, then trying to get a Javascript engine should return null. Calling engine.eval ( foo ); with engine == null is going to throw a NullPointerException. |
|
|
|
|
|
andrew.
|
Posted: Thu Jun 04, 2009 6:59 am Post subject: RE:ScriptEngineManager |
|
|
Well, I just moved to my Linux machine and that's what I do my programming on now so there should be no errors. |
|
|
|
|
|
|
|