1

Hey guys I have a heroku web app with a PostgreSQL database. As you can see in the picture the DATABASE_URL is set:

ev

enter image description here

But for some reason when I try access the environment variable to connect to the database I get a NullPointerException because apparently the DATABASE_URL does not exist:

private static Connection getConnection() throws URISyntaxException, SQLException {
  URI dbUri = new URI(System.getenv("DATABASE_URL"));

  String username = dbUri.getUserInfo().split(":")[0];
  String password = dbUri.getUserInfo().split(":")[1];
  String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

  return DriverManager.getConnection(dbUrl, username, password);
}

error

What's going on here? How can I fix it?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Savan Luffy
  • 440
  • 3
  • 12
  • What's the exact error and traceback? We need to know what, specifically, is generating the NPE. – ChrisGPT was on strike Apr 20 '19 at 01:07
  • 1
    Also, you've leaked a significant portion of your Postgres connection string (your entire username and a good chunk of your password). Before you do anything else, please [rotate your credentials](https://devcenter.heroku.com/articles/heroku-postgresql#pg-credentials). The ones in your screenshot are forever compromised. – ChrisGPT was on strike Apr 20 '19 at 01:09
  • @Chris this is just a test database I want use it for later purposes. The error occurs in the first line of code System.getenv cant find my database_url environment – Savan Luffy Apr 20 '19 at 01:11
  • Okay, now you've leaked two entire connection strings. Even if they're test databases, _please rotate their credentials **immediately**_. – ChrisGPT was on strike Apr 20 '19 at 01:18
  • Don't describe the error, _show it to us_. [`java.lang.System.getenv(name)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#getenv(java.lang.String)) should only throw `NullPointerException` if `name` is `null`. If the value isn't set this call should _return_ null. If that's what's happening, your error occurs when you call `.getUserInfo()` on it, or maybe when you instantiate a `URI` from it. But we shouldn't have to guess… please give us the exact error. See [ask]. – ChrisGPT was on strike Apr 20 '19 at 01:22
  • @Chris yeah I changed the passwords of my databases. well yeah exactly. System.geenv(".."); returns a null value, because the url is not set. But how can it not be set when there is an environmental variable called "DATABASE_URL" – Savan Luffy Apr 20 '19 at 01:26
  • For the _last time, please **show us the exact error message**_. We're not going to take your word for what you think the problem is. – ChrisGPT was on strike Apr 20 '19 at 01:28
  • @Chris like the picture I edited? – Savan Luffy Apr 20 '19 at 01:30
  • That doesn't look like a regular Heroku build to me. What command are you running to generate that? – ChrisGPT was on strike Apr 20 '19 at 01:39
  • @Chris I am executing it via netbeans – Savan Luffy Apr 20 '19 at 01:41

2 Answers2

1

Heroku config vars are environment variables. They're defined in and apply to a particular environment, in this case the environment in which an app runs on Heroku. They have no effect on other environments, for example ones on the machine sitting in front of you.

You can configure NetBeans to use an environment variable for your project locally. The value of the DATABASE_URL here can be different from what it is on Heroku, just make sure to set it to a valid PostgreSQL URL value.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    does that mean I can't access my database_url in the deployed heroku web app? because now I deployed my app on heroku and I used a test environmental variable to show it on the webpage but it does not show – Savan Luffy Apr 20 '19 at 15:35
  • @SavanLuffy, be careful with casing. Environment variables called `DATABASE_URL` and `database_url` are different. The variables you see via `heroku config` should be available in the apps you provide with the `-a` / `--app` option. – ChrisGPT was on strike Apr 20 '19 at 15:38
0

For Windows

write set DATABASE_URL=url address from Heroku

For macOS

export DATABASE_URL=url address from Heroku

Make sure you have installed PostgreSQL first.

Eric Ly
  • 2,095
  • 1
  • 20
  • 27