0

I am trying to connect to my DBeaver database through eclipse but it doesn't work. I am trying to read from my application.resources file in my resources folder, but when i try to connect, it says that my property variables are null.

This is how my directory looks like directory

Here is the full stacktrace

Caused by: java.lang.NullPointerException: inStream parameter is null
    at java.base/java.util.Objects.requireNonNull(Objects.java:233)
    at java.base/java.util.Properties.load(Properties.java:407)
    at com.revature.utils.DAOUtility.<clinit>(DAOUtility.java:21)

This is my code:

package com.revature.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class DAOUtility {
    private static String CONNECTION_USERNAME;
    private static String CONNECTION_PASSWORD;
    private static String CONNECTION_URL;
    private static Connection connection;
    
    static {
        try {
            InputStream input = ClassLoader.getSystemResourceAsStream("application.properties");
            Properties properties = new Properties();
            properties.load(input);
            
            String SYSTEM_VARIABLE_USERNAME = properties.getProperty("USERNAME_PROPERTY");
            String SYSTEM_VARIABLE_PASSWORD = properties.getProperty("PASSWORD_PROPERTY");
            String SYSTEM_VARIABLE_URL = properties.getProperty("URL_PROPERTY");
            
            CONNECTION_USERNAME = System.getenv(SYSTEM_VARIABLE_USERNAME);
            CONNECTION_PASSWORD = System.getenv(SYSTEM_VARIABLE_PASSWORD);
            CONNECTION_URL = System.getenv(SYSTEM_VARIABLE_URL);
            
            input.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public static Connection getConnection() throws SQLException {
        // not necessary if it can be found in classpath
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Could not register driver!");
            ex.printStackTrace();
        }
        
        if (connection == null || connection.isClosed()) {
            System.out.println(CONNECTION_URL);
            //System.out.print(new File("").getAbsolutePath());
            connection = DriverManager.getConnection(CONNECTION_URL, CONNECTION_USERNAME, CONNECTION_PASSWORD);
        }
        
        return connection;
    }
    
    public static void main(String[] args) {
        // try to register our driver: optional
        try {
            // registers a driver to driver manager: class responsible for getting a connection
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException ex) {
            System.out.println("Could not register driver!");
            ex.printStackTrace();
        }
        
        try {
            Connection connection = DriverManager.getConnection(CONNECTION_URL, CONNECTION_USERNAME, CONNECTION_PASSWORD);
            System.out.println("Connection is valid: " + connection.isValid(5));
        } catch (SQLException ex) {
            System.out.println("Connection attempt failed!");
            ex.printStackTrace();
        }
    }
}

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.revature</groupId>
  <artifactId>bank-project</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>bank-project</name>
  <description>Bank Project for RAP</description>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>16</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <!-- create 'dependencies' element -->
    <dependencies>
        
        <!-- Add in any dependencies -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.3.0</version>
        </dependency>
    </dependencies>
</project>

I have changed the resources folder from a regular folder to a source folder and moved it inside my "src" folder but i am still getting the null error

  • 1
    The error is because it cant find the application.properties in the classpath. If you are running through Eclipse, you need to explicitly point the resources folder in the Java Run configuration classpath. – Shankar Jan 05 '22 at 04:24
  • 1
    I figured it out, instead of having my environment variables stored in my .bash_profile I had to store them in Eclipse. [link](https://stackoverflow.com/questions/7048216/environment-variables-in-eclipse) this page helped me out – ivan villasenor Jan 05 '22 at 14:00

0 Answers0