Java.util.Properties $ LineReader.readLine

I need to read the configuration file. I get this error when running the following code:

java.util.Properties$LineReader.readLine

The config.cfg file is present and has r / w permissions.

 import java.util.*; import java.util.Properties; public class Config { Properties configFile; public Config() { configFile = new java.util.Properties(); try { configFile.load(this.getClass().getClassLoader(). getResourceAsStream("config.cfg")); }catch(Exception eta){ eta.printStackTrace(); } } public String getProperty(String key) { String value = this.configFile.getProperty(key); return value; } } 

EDIT - Complete Error

  [java] java.lang.NullPointerException [java] at java.util.Properties$LineReader.readLine(Properties.java:418) [java] at java.util.Properties.load0(Properties.java:337) [java] at java.util.Properties.load(Properties.java:325) [java] at Config.<init>(Unknown Source) [java] at ClosureBuilder.<clinit>(Unknown Source) 

EDIT - Directory Structure

Src

-> config.java

-> config.cfg

+4
source share
4 answers

You should put your config.cfg in the same folder where your .class file is located.

+7
source

The resource stream is returned as null. A resource is not where you think it is on its way to classes.

0
source

Check the path you get for your configuration using this.getClass (). getResource ("/");

0
source

It seems your program cannot find the config.cfg file.

 this.getClass().getClassLoader().getResourceAsStream("config.cfg") 

The above call returns null .

Try the following:

 InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.cfg") if(is !=null){ configFile.load(is); } 

This change will not allow your program to crash. But also, if the file is not located, your configFile property object will not have any property from the file.

-1
source

Source: https://habr.com/ru/post/1413886/


All Articles