1

I would like to connect to a Heroku Database and I tried to replicate the model like their tutorial: https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-the-jdbc_database_url

When below line is executed

URI dbUri = new URI(System.getenv("postgresql://osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"));

I am getting the error

java.lang.NullPointerException
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JamesB
  • 569
  • 1
  • 9
  • 31

1 Answers1

2

When you create the URI object, you're using System.getenv(), which attempts to get the value for a system environment variable called "postgresql://osnvehq...", which most certainly does not exist and was not your intention. Remove the System.getenv() and just use the url string.

Suggestion: Before the return with the connection, you might put in a few System.out.println() statements with your variables and check the console output to make sure the URL, username, and password is being extracted correctly from the URI.

Also, please say those aren't the real user and password to your database which are now exposed to the whole world... If those are real, you need to change them immediately - not here on SO, that cat is out of the bag already, change it in your actual database.

EDIT: Ok, I just looked at the heroku link, and what that tutorial is telling you is to use new URI(System.getenv("DATABASE_URL")) and leave the DATABASE_URL part as-is. That's an environment variable what will contain the actual connection information.

Mark Olsson
  • 320
  • 2
  • 8
  • I can just delete it this DB is for test purposes, and it's empty. – JamesB Aug 17 '16 at 02:44
  • I understood nothing in the first part, I don't know what is System.getenv(), I already tryed return DriverManager.getConnection(URL, user, pass) but I don't know the URL everything that heroku passes in dashboard is the URL postgres://osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv that's does not seen like a URL. – JamesB Aug 17 '16 at 02:50
  • How can I get this specific URL to get connection method? Thanks. – JamesB Aug 17 '16 at 02:50
  • Read up on environment variables. They're a little different for each OS (win/mac/linux), so their use will be dependent on what system you are on. I've never used Heroku, so I don't know how it connects to the database. But apparently the info is stored in the environment variable DATABASE_URL, though I don't know how it gets set. So in your program, use `System.getenv("DATABASE_URL")` to get the contents of the variable for the connection. Or just temporarily hard code the connection info without using getenv() to get up and running. – Mark Olsson Aug 17 '16 at 03:37
  • 1
    All I can say for sure is `URI(System.getenv("postgresql://osnvehqhufnxzr:TS3Qt37c_HHbGRNKw3yk7g88fp@ec2-54-225-93-34.compute-1.amazonaws.com:5432/d39mfq0odt56bv?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"));` is not correct. It should probably be `URI(System.getenv("DATABASE_URL"))`, but You haven't provided enough information to be sure. Try and follow the instructions carefully on that devcenter.heroku.com link you provided. – Mark Olsson Aug 17 '16 at 05:50
  • @JamesB: "I don't know what is System.getenv()" - that should be a red flag to you. Never just use methods without knowing what they do. Read up on the documentation for `System.getenv` to find out what it does. – Jon Skeet Aug 17 '16 at 06:27
  • If heroku tutorial says I have to use this method to connect to their database I will use of course, I don't have to know what it does, it's not my purpose, and nobody really seems to know it, I you had a look in the discussion you wouldn't down voted. I believe I should find another server that's the easier way. – JamesB Aug 17 '16 at 17:10