NullPointerException when reading a properties file in Java

前端 未结 11 1341
我在风中等你
我在风中等你 2020-12-09 17:05

I am using the following code to read a properties file:

Properties pro = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().         


        
相关标签:
11条回答
  • 2020-12-09 17:13

    Perhaps, I have plucked all my hairs out and going then I have found this solution out:

    Properties dbParamProperties = new Properties();
             InputStream input = null;
            try {
    
                String pathOfAbsolute = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
                String propertiesFilePath = pathOfAbsolute+"/properties/conf.properties";
                propertiesFilePath = propertiesFilePath.replace("file:/", "").replace("/", "\\");
               System.out.println(pathOfAbsolute);
               System.out.println(propertiesFilePath);
               Paths.get(new URI(pathOfAbsolute));
                 input =  ClassLoader.getSystemResourceAsStream(propertiesFilePath);
               input = new FileInputStream(propertiesFilePath);
               dbParamProperties.load( input );
               dbUID =  dbParamProperties.getProperty("userName");
               dbURL =  dbParamProperties.getProperty("hosturl");
               dbPWD =  dbParamProperties.getProperty("password");
               dbPort = dbParamProperties.getProperty("port");
               dbSID =  dbParamProperties.getProperty("servicenameorsid");
    
    
            } catch (IOException e) {
    
                e.printStackTrace();
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
    
    0 讨论(0)
  • 2020-12-09 17:14

    I had the same problem and this helped me:

    InputStream is;
    try {
    
        is = this.getClass().getClassLoader().getResourceAsStream("config.properties");
    
        prop.load(is);
    
        String url = prop.getProperty("url");
        String user = prop.getProperty("user");
        String pass = prop.getProperty("password");
        is.close();
        // opening database connection to MySQL server
        con = DriverManager.getConnection(url, user, pass);
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-12-09 17:15

    In my situatioan I get NullPointerException in this code

    LogManager.getLogManager()
          .readConfiguration(MyClass.class.getResourceAsStream("config/logging.properties"));
    

    I changed

    LogManager.getLogManager().readConfiguration(AzLotteryTerm.class.getClassLoader().getResourceAsStream("config/logging.properties"));
    

    and now works ok!

    0 讨论(0)
  • 2020-12-09 17:17

    I had the same problem and I have resolved by doing the following

    1. File file = new File("resources.properties");
    2. System.out.println(file.getAbsolutePath());

    and then put "resources.properties" file under that path.

    0 讨论(0)
  • 2020-12-09 17:18

    Many seem to have this problem and like me they give up after sometime. Here is what I had to get this working. The trick here to use relative path for file lookup is to make sure your classes folder contains resources files along with src files. Here is what I ended up doing.

    1) If you are using eclipse make sure you proper .classpath setting present and do PROJECT CLEAN to see the resources files get generated under /classes. Notice the classpath-entries below for resource files place under src/main/resource

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
        <classpathentry including="**/*.java" kind="src" path="src/main/java"/>
        <classpathentry kind="var" path="M2_REPO/javax/mail/mail/1.4.4/mail-1.4.4.jar"/>
        <classpathentry kind="var" path="M2_REPO/javax/activation/activation/1.1/activation-1.1.jar"/>
        <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="output" path="target/classes"/>
    </classpath>
    

    2) If you are using maven as well make sure you configure your pom.xml as per the https://maven.apache.org/guides/introduction/introduction-to-the-pom.html and do mvn clean install to see the files under target/classes

    3) Once you have got the resource files under /classes the next thing to do in java is the following. Don't forget to have the forward slash.

    try {
                properties.load(getClass().getResourceAsStream("/mail-config.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    I could have added some images but did not have points. :)

    0 讨论(0)
  • 2020-12-09 17:19

    I had this problem with a third-party program and it turned out that I needed to include . in the classpath so that the program could read a local properties file in the current working directory.

    0 讨论(0)
提交回复
热议问题