2

Im tring to run this example from the Renjin website, http://www.renjin.org/documentation/developer-guide.html , im tring to run the first "A simple primer" example.

following is my directory layout:

enter image description here

And here is my code:

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);     

    }
}

Why am i getting the following Exception? (i should get of 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)

thanks in advance

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Yehoshaphat Schellekens
  • 2,305
  • 2
  • 22
  • 49

1 Answers1

2

The exception is throw because your application can't find the Renjin ScriptEngine. You have provided renjin-studio as a library, but you need the renjin-script-engine library which is available from http://build.bedatadriven.com/job/renjin/lastSuccessfulBuild/org.renjin$renjin-script-engine/ (use the JAR with dependencies).

Unfortunately ScriptEngineManager.getEngineByName() only returns null if it can't find the engine so you can add the following check to ensure that the engine has loaded:

// check if the engine has loaded correctly:
if(engine == null) {
    throw new RuntimeException("Renjin Script Engine not found on the classpath.");
}

Also note: it is called Renjin, not Rengin!

mjkallen
  • 468
  • 3
  • 12
  • Sorry for the Rengin mistake, ill check the code on monday and let you know. btw, this project is really cool, the `JRI` alternative is terrible. – Yehoshaphat Schellekens May 21 '14 at 12:52
  • and there it works: run: The result of a*b is: 6.0 BUILD SUCCESSFUL (total time: 1 second) – Yehoshaphat Schellekens May 21 '14 at 12:59
  • I must say i noticed that both you and your partner, arn't much into stack overflow, notice that all of the r guru's use this site to promote their packages, i think it would be good for your product as well. – Yehoshaphat Schellekens May 21 '14 at 13:03
  • 1
    Glad it now works and that you're excited about the project! Regarding our presence on SO: there are not that many questions being asked here that concern Renjin. We have a [Google Group](http://groups.google.com/group/renjin-dev) that is quite active. It's also easier to keep track of questions there as we get an e-mail notification whenever someone posts a message. – mjkallen May 21 '14 at 17:48
  • i guess ill just take care of the questions :) – Yehoshaphat Schellekens May 21 '14 at 20:34
  • ill ask some colleagues with high reputation to create the tag `Renjin` – Yehoshaphat Schellekens May 21 '14 at 20:48