试图从Renjin
网站http://www.renjin.org/documentation/developer-guide.html运行此示例,试着运行第一个“简单的入门”示例。
以下是我的目录布局:
这是我的代码:
package stackoverflow;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.renjin.sexp.*; // <-- import Renjin's object classes
/**
*
* @author yschellekens
*/
public class StackOverflow {
public static void main(String[] args) throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
// create a Renjin engine
ScriptEngine engine = factory.getEngineByName("Renjin");
// evaluate R code from String, cast SEXP to a DoubleVector and store in the 'res' variable
DoubleVector res = (DoubleVector)engine.eval("a <- 2; b <- 3; a*b");
System.out.println("The result of a*b is: " + res);
}
}
为什么我会收到以下异常? (我应该得到6)
run:
Exception in thread "main" java.lang.NullPointerException
at stackoverflow.StackOverflow.main(StackOverflow.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
提前致谢
答案 0 :(得分:2)
抛出异常,因为您的应用程序无法找到Renjin ScriptEngine。您已将renjin-studio作为库提供,但您需要可以从http://build.bedatadriven.com/job/renjin/lastSuccessfulBuild/org.renjin$renjin-script-engine/获得的renjin-script-engine库(使用带有依赖项的JAR)。
不幸的是ScriptEngineManager.getEngineByName()
只返回null
,如果它找不到引擎,那么您可以添加以下检查以确保引擎已加载:
// check if the engine has loaded correctly:
if(engine == null) {
throw new RuntimeException("Renjin Script Engine not found on the classpath.");
}
另请注意:它被称为 Renjin ,而不是 Rengin !