0

I have the following implementation:

public static void main(String[] args) {
    // new R-engine
    Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
    if (!re.waitForR()) {
        System.out.println("Unable to load R");
        return;
    } else
        System.out.println("Connected to R");

    re.eval("library(RMySQL)");
    re.eval("library(xts)");
    re.eval("library(forecast)");
    re.eval("db = dbConnect(MySQL(), user='root', password='', dbname='stats_server', host='localhost')");
    re.eval(
        "data = fetch(dbSendQuery(db, \"select date(time_stamp) as date, total from history_data where discriminator='D' and hour_of_day=3\"),n=-1)");
    re.eval("data$date = as.Date(as.character(data$date), format(\"%Y-%m-%d\"))");
    re.eval("dataAsTimeSeries = xts(data$total, data$date)");
    re.eval("forecastedData = forecast(auto.arima(dataAsTimeSeries), h=20)");
    REXP mean = re.eval("forecastedData$mean");
    double[] foreCast = mean.asDoubleArray();

    for (int i = 0; i < 10; i++) {
        System.out.println(foreCast[i]);
    }
    re.end();
}

The line double[] foreCast = mean.asDoubleArray() throws NullPointerException, because mean is null. I can't figure out why re.eval("forecastedData$mean") returns null.

UPDATE: After a long investigation, I found out that it is not loading the libraries (RMySQL, xts, forecast). I have added the libraries to PATH environment variable but still they are not discovered.

Rahul Sharma
  • 892
  • 1
  • 6
  • 16
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Seth Jul 29 '16 at 13:28
  • @Seth rephrased the question. hope you understand the problem now – Rahul Sharma Jul 29 '16 at 13:40
  • Hm. I get it now, I cannot figure out the issue though. The docs state that it returns null "if something wnet (sic!) wrong", so... :/ – Seth Jul 29 '16 at 13:41
  • I tried to execute the same statements in RGui, and everything works fine. – Rahul Sharma Jul 29 '16 at 13:44
  • That's annoying. Did you try leaving the other re.eval statements out & just executing the last one? It might return null due to the rengine being busy. – Seth Jul 29 '16 at 13:45
  • @Seth just executing the last statement wouldn't work because the value comes from executing the previous statements. – Rahul Sharma Jul 29 '16 at 13:57

0 Answers0