Reputation: 29
When I run the code below:
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
InputStream stream = new FileInputStream(new File(System.getProperty("D:\\src\\objects\\object.properties")));
//load all objects
p.load(stream);
return p;
}
It shows error as:
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at uioperation.Excel_object.getObjectRepository(Excel_object.java:14)
at ExecuteTestcase.Testcase_execute.main(Testcase_execute.java:27)
What is wrong with my code?
Upvotes: 0
Views: 106
Reputation: 436
Seems your code for setting the property is not implemented, But you can simply Open a file using following code.
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
String FileName="Path to your file";
InputStream stream = new FileInputStream(new File(fileName));
//load all objects
p.load(stream);
return p;
}
Or otherwise, you should get it from system properties..
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
String FileName=System.getProperty("Prperty Key for your file path"); // the property should ave been already set somewhere in the code before execution of this line
InputStream stream = new FileInputStream(new File(fileName));
//load all objects
p.load(stream);
return p;
}
This is for setting the property and accessing the file
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Set Property for file path
setProperty("filePath","D:\\src\\objects\\object.properties");
//Read object repository file
String FileName=System.getProperty("filePath"); //now fileName is similar to "D:\\src\\objects\\object.properties"
InputStream stream = new FileInputStream(new File(fileName));
//load all objects
p.load(stream);
return p;
}
Upvotes: 0
Reputation: 44813
I do not believe that you have a System property called D:\\src\\objects\\object.properties
so
change to
new File("D:\\src\\objects\\object.properties"));
assuming that this file does exist
Upvotes: 2