我第一次使用Groovy和Java,并在运行简单的主项目时不断出现异常。
线程中的异常" main" java.lang.NullPointerException:无法在null对象
上调用方法sell()
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();
}
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"
}
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);
}
答案 0 :(得分:0)
发现问题,TradeDsl买入,卖出,需要退货TradeDsl