I am using Groovy and Java for the first time and keep getting exception while running my simple main project.
Exception:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke method sell() on null object
class App Java:
public void start() throws CompilationFailedException, IOException {
File trades = uploadGroovyScript();
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(trades);
TradeDsl tradeDsl = new TradeDsl();
Closure c = (Closure) binding.getVariable("trades1");
c.setDelegate(tradeDsl);
c.call();
}
file Groovy:
trades1 = {
buy 100,200 from "MSFT"
buy 1000,200 from "APPL"
buy 500,200 from "VMW"
sell 50,200 from "MSFT"
buy 1200,200 from "MSFT"
sell 200,200 from "VMW"
buy 1200,200 from "APPL"
}
class TradeDsl:
public void buy(int quantity1 , int quantity2) {
System.out.println("Buying " + quantity1 + " or " + quantity2);
}
public void sell(int quantity1) {
System.out.println("Selling " + quantity1);
}
public void from(String epic) {
System.out.println("from " + epic);
}