Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3451650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:10:06+00:00 2026-05-18T09:10:06+00:00

I need to write a web-aplication that connects to database and reads connections properties

  • 0

I need to write a web-aplication that connects to database and reads connections properties from .properties file.
I use Maven and I want to write some unit test for my app.
To test classes that works with database I put my .properties file to src/main/resources and to test/main/resources and wrote the following code for reading properties:

package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;


/**
 *
 * @author proger
 */
public class ProgrammSettings {

    private static Logger logger = Logger.getRootLogger();
    private static String propertiesPath = "program.settings.properties";

    private static Properties props;

    public static String getProperty(String key) {
        if (props == null)
            loadProperties();

       return props.getProperty(key);

    }

    public static void setResourcesFilePath(String path) {
        propertiesPath = path;
    }

    private static void loadProperties() {
        InputStream propOut = null;
        try {
            //URL url =  ClassLoader.getSystemResource(propertiesPath);
            propOut = ClassLoader.getSystemClassLoader().getResourceAsStream(propertiesPath); //new FileInputStream(url.getFile());
            props = new Properties();
            props.load(propOut);
        } catch (Exception ex) {
            //Logger.getLogger(ProgrammSettings.class.getName()).log(Level.SEVERE, null, ex);
            String errorMessage = "Error during properties file reading. Path to file: '" +
                           propertiesPath + "'";
            logger.error(errorMessage, ex);
            throw new RuntimeException(errorMessage, ex);
        } finally {
            try {
                if (propOut != null)
                    propOut.close();
            } catch (IOException ex) {
                logger.error("Error during closing stream from properties file", ex);
            }
        }
    }
}

I wanted the same code worked both for tests and app classes, so I read properties file from classPath.
All my tests pass and my .properties file is packed into war by Maven. But when I run my App from Netbeans this I receive:

java.lang.NullPointerException
    java.util.Properties$LineReader.readLine(Properties.java:435)
    java.util.Properties.load0(Properties.java:354)
    java.util.Properties.load(Properties.java:342)
    util.ProgrammSettings.loadProperties(ProgrammSettings.java:49)
    util.ProgrammSettings.getProperty(ProgrammSettings.java:33)
    dataacess.AbstractDataMapper.loadDriver(AbstractDataMapper.java:58)
    dataacess.AbstractDataMapper.<init>(AbstractDataMapper.java:41)
    dataacess.UserMapper.<init>(UserMapper.java:25)
    servlets.UserRegistration.processRequest(UserRegistration.java:86)
    servlets.UserRegistration.doPost(UserRegistration.java:132)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

How can I fix it? Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:10:06+00:00Added an answer on May 18, 2026 at 9:10 am

    Place program.settings.properties in webapps/appname/WEB-INF/classes

    And you can use a simpler line ProgrammSettings.class.getResourceAsStream(..) (i.e. not the system classloader, but the current classloader)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.