2

I am trying to use the REngine.GetInstance() function but I keep getting a null reference exception.

I have tried using another function in REngine just in case the getInstance method was at fault, like REngine.SetEnvironmentVariables(), yet they all return the null reference exception.

I have tried reinstalling the package. I have tried checking the installation path but I couldn't find how the rdotnetlibrary accesses it. I am not even sure the path is related to the problem.

Please help.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
Mat
  • 41
  • 1
  • 5
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Backs Apr 23 '18 at 14:09
  • i know what is a nullreferenceexception. the thing is, i'm using a third party library, that library contains a class, and the instance of that class is not supposed to be null when i access it after importing the library. – Mat Apr 23 '18 at 14:15
  • also, the library uses a certain dll, i cannot see the functions of the dll, i can only see an interface they build to access those functions. – Mat Apr 23 '18 at 14:18
  • 1
    Could you provide us a simple sample how did you use the unctionREngine.GetInstance(), otherwise, we will not know this issue is related to the r.net or your script? – Joy Apr 24 '18 at 03:28
  • 1
    Maybe you need to call or initialize something first.. – Tasos K. Apr 24 '18 at 05:29
  • Check that you are not accessing a 64 bit R installation from a 32 bit process – Jesper Mygind Sep 06 '18 at 08:40

4 Answers4

6

Make sure your startupparameters are set up correctly. Since you do not provide us enough information; this is a correct way to get r.net's REngine running:

//...
StartupParameter rinit = new StartupParameter();
rinit.Quiet = true;
rinit.RHome = "C:/Program Files/R/R-3.4.3";
rinit.Interactive = true;
REngine.SetEnvironmentVariables();
rMain = REngine.GetInstance(null, true, rinit);
//...

Make sure you setup RHome to the correct installed R path.

EDIT (thanks to @David M.): In usual cases you only need to pass StartupParameter to GetInstance() if you don't want to have default initialization settings. However, according to the source code comments for the first parameter:

The file name of the library to load, e.g. "R.dll" for Windows. You usually do not need need to provide this optional parameter

In rare cases you need to provide the path of R.dll:

//...
rMain = REngine.GetInstance("C:/Program Files/R/R-3.4.3/bin/x64/R.dll", true, rinit);
//...
Shique
  • 724
  • 3
  • 18
  • 1
    These are typical initializer statements; one option would be to set this in a constructor of your rdotnet class, or your main() of you want to keep it small – Shique Jul 20 '18 at 17:18
  • 1
    This solution almost worked for me. I'm not clear why, but on my system I also had to specify the path to the `R.dll` file instead of leaving it null, i.e. `REngine.GetInstance("C:/Program Files/R/R-3.4.4/bin/x64/R.dll", true, rinit);` – David M. Aug 29 '19 at 19:22
  • 1
    Thanks @DavidM. I edited my answer. The source code does not provide any useful information why the location of `R.dll` had to be specified – Shique Aug 30 '19 at 08:05
3

I've had the same issue using version 3.5.0

a call to "REngine.GetInstance" would result in 'Object reference not set to an instance of an object'

I downgraded to 3.4.0 and I'm not getting that error anymore.

  • Same problem here, but I am stuck on R 3.5 for other reasons. Is there a workaround? – Karl Oct 19 '18 at 20:49
0

When we upgraded R from 3.4 to 3.5, we got that exact error. We downgraded back to 3.4 and moved on.

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Chris Lincoln
  • 368
  • 4
  • 13
0

As other answers have pointed out, this appears to be an issue relating to R 3.5 and above. I also managed to work around this by downloading R 3.4.4 and having both versions run concurrently, using Shique's solution.

For those unable to downgrade their R, it looks like jmp75 has been working on a fix, and there is a WIP branch available at https://github.com/StatTag/rdotnet/tree/r_3_5_0

CrowsNose
  • 83
  • 1
  • 10