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?

5
  • Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Aug 17, 2016 at 1:05
  • @Jonathon Reinhart Aff. Commented Aug 17, 2016 at 1:07
  • 1
    why are you using System.getenv ? This just seems to be a normal string Commented Aug 17, 2016 at 1:18
  • 1
    Probably not a good idea to include your actual DB credentials, you think? Commented Aug 17, 2016 at 1:40
  • Run heroku pg:credentials DATABASE --reset Commented Aug 17, 2016 at 2:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I can just delete it this DB is for test purposes, and it's empty.
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.
How can I get this specific URL to get connection method? Thanks.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.