OHDSI Home | Forums | Wiki | Github

DatabaseConnector::connect gives Error in rJava

Hi,
I try to connect to my PostgreSQL server using DatabaseConnector::connect, but I get the next error:

> conn <- DatabaseConnector::connect(connectionDetails)
Connecting using PostgreSQL driver
Error in rJava::.jcall(p, "Ljava/lang/Object;", "setProperty", names(properties)[I],  : 
  java.lang.NullPointerException

I couldn’t find a solution online. Can someone help me with that?
I tried removing and reinstalling java, rjava, DatabaseConnector, but nothing helped.

Hi @nadavrap

Here you must download the jars to connect to your database. Follow this link it will guide you on how to do that.

https://ohdsi.github.io/DatabaseConnectorJars/

If you need any further help let me know.

Kind regards,
Dinuja S’Wickrama

Obviously, I already downloaded dbms for postgresql. I tried doing so again, but the same error appears. Here are the steps that generate the error:

> DatabaseConnector::downloadJdbcDrivers('postgresql')
trying URL 'https://ohdsi.github.io/DatabaseConnectorJars/postgresqlV42.2.18.zip'
Content type 'application/zip' length 931791 bytes (909 KB)
==================================================
downloaded 909 KB

DatabaseConnector JDBC drivers downloaded to '/home/user1'.
> connectionDetails <- DatabaseConnector::createConnectionDetails(dbms = dbms,
+                                                                 server = server,
+                                                                 user = user)
> conn <- DatabaseConnector::connect(connectionDetails)
Connecting using PostgreSQL driver
Error in rJava::.jcall(p, "Ljava/lang/Object;", "setProperty", names(properties)[I],  : 
  java.lang.NullPointerException

Hi @nadavrap,

Can you tell me which java version are you using?
It is working fine with the below steps.

My Steps:

  1. Installed Java Version 8(JDK 8u211 and later)
  2. install.packages(“rJava”)
  3. Sys.setenv(JAVA_HOME=“C:/Program Files/Java/jdk1.8.0_301/”)
  4. install.packages(“DatabaseConnector”)
  5. Sys.setenv(“DATABASECONNECTOR_JAR_FOLDER” = “c:/temp/jdbcDrivers”)
  6. DatabaseConnector::downloadJdbcDrivers(‘postgresql’)
  7. connectionDetails ← DatabaseConnector::createConnectionDetails(dbms = “postgresql”,server = “server/database”,user = “postgres”,password=“admin”)
  8. conn ← DatabaseConnector::connect(connectionDetails)

Screenshot 2021-11-17 192416

Something is weird, I do have rJava, and I can load it. But when I tried to reinstall it, I get the next error:

Error: package or namespace load failed for ‘rJava’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/home/user1/R/x86_64-koji-linux-gnu-library/4.0/00LOCK-rJava/00new/rJava/libs/rJava.so':
  libiconv.so.2: cannot open shared object file: No such file or directory

So I found libiconv.so.2, and add it to the ld path, but then I get another error when I try to reinstall rJava:

Error: package or namespace load failed for ‘rJava’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/home/user1/R/x86_64-koji-linux-gnu-library/4.0/00LOCK-rJava/00new/rJava/libs/rJava.so':
  /home/user1/R/x86_64-koji-linux-gnu-library/4.0/00LOCK-rJava/00new/rJava/libs/rJava.so: undefined symbol: JNI_GetDefaultJavaVMInitArgs
Error: loading failed

So maybe it’s related to some other installations (working on Linux). Do you have any clue?

Hi @nadavrap,

I did the same thing on ubuntu using OracleJDK version 8, it is working fine. Can you kindly tell me which java versions are you using OpenJDK or Oracle JDK?

So, I tried it again after this incident, it is working fine with my steps.

Screenshot 2021-11-18 222638

Screenshot 2021-11-18 222912

Hopefully, the problem is with the OpenJDK. if you need further help to install Oracle JDK. Let me know.

Kind regards,
Dinuja

Unfortunately, it still doesn’t work for me. I’m working on an EC2 Centos instance. I tried to install directly, or from anaconda, I tried the open JDK, and I tried Oracle JDK (couldn’t find for anaconda). But still I’m getting the same error of:

java.lang.NullPointerException

Does anyone have a conda environment that works OHDSI packages?

Could you give a bit more context around the error message? Is this still when calling DatabaseConnector::connect()? Are there any other messages?

There are no other error messages. createConectionDetails works, but DatabaseConnector::connect` gives the next error message:

> DatabaseConnector::connect(connectionDetails)
Connecting using PostgreSQL driver
Error in rJava::.jcall(p, "Ljava/lang/Object;", "setProperty", names(properties)[i],  : 
  java.lang.NullPointerException

Are you using the Postgres JDBC driver installed by downloadJdbcDrivers()?

Yes

I must confess I’m at a loss. The error message suggests p is null here, but it is created right here, and therefore cannot be null.

Even though you appear to be connected to Java, the Java is behaving oddly.

Can you please help me understand what configuration works for you? Which R and Java version, as well as other essential R packages’ version?
Alternatively, how can I debug that?

I’m running Windows, with Oracle Java 1.8, R 4.1.2. We’ve got unit tests set up on MacOS and Ubuntu as well (see here). I’m not exactly sure what Java those are running.

If you’d like to try to debug, maybe you could try running this code? This seems to be causing a problem in your setup (in mine it just produces ‘NULL’ as output)::

rJava::.jinit()
p <- rJava::.jnew("java/util/Properties")
rJava::.jcall(
  p,
  "Ljava/lang/Object;",
  "setProperty",
  "test",
  "test")
)

Alternatively, if you know your way around R, you could try debugging the problematic function, and see what values are used:

debug(DatabaseConnector:::connectUsingJdbcDriver)
# And now run the code that causes the issue

Thank you for your help. By debugging I found that my password wasn’t set at all, but was NULL… :roll_eyes: Though, the error message was misleading…

So to conclude:
If properties$password is NULL, the next line in connectUsingJdbcDriver function:

rJava::.jcall(p, "Ljava/lang/Object;", "setProperty", 
        names(properties)[i], as.character(properties[[i]])[1])

returns the next error:

Error in rJava::.jcall(p, "Ljava/lang/Object;", "setProperty", names(properties)[i],  : 
  java.lang.NullPointerException

Thanks for digging into this!

I agree the error message should have been more informative. I’ll work on that.

Now in the develop branch: https://github.com/OHDSI/DatabaseConnector/commit/f974073b58f573d9f28819befa80b74b70e3e010

t