Reputation: 688
I have main Class
which is TestDriver.java
, In which I am calling functions
such as read to excel files and all and for that, I need to give an excel file path in java so instead of hardcoding I am using Properties file and using that I am Calling my function.
For reading properties file I have given file path but I am unable to read properties file...
File: TestDriver.java
public class TestDriver
{
public static void main(String args[]){
String filePath = "E:\\Project\\HRMSAutomation\\";
Globals gbl = new Globals(filePath);
// Functions.....
}
}
File : Globals.java
public class Globals
{
static String filePath;
public Globals(String path) {
this.filePath = path;
}
static Properties prop = Genlib.readConfig(filePath+"config.properties");
//readConfig is a method which will read the properties file
public static final String LOGIN_URL = prop.getProperty("loginUrl");
}
File : Config.properties
loginUrl = someurl....
testsRoot = tests
configFileDir = E:/Project/Automation/Data/ConfigData/
Error Log.
java.io.FileNotFoundException: nullconfig.properties (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at gtlib.Genlib.readConfig(Genlib.java:96)
at projlib.Globals.<clinit>(Globals.java:15)
at testDrivers.TestDriver.main(TestDriver.java:89)
I am getting Error because filePath variable getting null value may be some mistake in Globals.java
file
Upvotes: 1
Views: 2200
Reputation: 23
Is the config.properties
file that exact name or Config.properties
?
in Globals.java
you refer to: filePath+"config.properties"
Upvotes: 0
Reputation: 1
I am not sure about Genlib but below is the simple way of loading property file. The path which you have to supply is relative path of the file. For example the file could be at
src/test/resources
so you just have to do it like below:
FileInputStream configFile = new FileInputStream("src/test/resources" + File.separator + "config.properties");
Properties configProperties = new Properties();
configProperties.load(configFile);
Upvotes: 0
Reputation: 1910
Im Java, static
fields' initialization runs before the constructor. So, when it initializes, filePath
is still null
. Place the initialization in the constructor like this:
public class Globals {
static String filePath;
public Globals(String path) {
this.filePath = path;
prop = Genlib.readConfig(filePath+"config.properties"); //readConfig is a method which will read the properties file
}
static Properties prop;
public static final String LOGIN_URL = prop.getProperty("loginUrl");
}
Also, if this is a static
class, I would recommend you to use a static
methis to initialize it and not a constructor.
Upvotes: 4
Reputation: 1954
Your file path is set in the constructor of the class, but your property object is using a static class variable, that is initialized when that variable is created.
So this means that when your property object is initialized, the file path is not set yet, so the properties is trying to open the filename of "nullconfig.properties".
Why are you storing those as static? If they are not necessary to be statics, you could move them all into class variables, and have the constructor open the property file instead.
Upvotes: 0
Reputation: 2648
Guess you are using plain java coding style.
by following code you can load property and retrieve certain parameter
Properties prop = new Properties();
Class clazz = Object.class;
InputStream stream = clazz.getResourceAsStream({path}/Config.properties"); prop.load(stream);
filePath = prop.getProperty("configFileDir");
In case you use SpringBoot, work become much easier
Upvotes: 1