0

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?

2
  • make sure the path is correct and the file "object.properties" is available. Commented Aug 22, 2014 at 5:49
  • 1
    There is no Property with the key "D:\\src\\objects\\object.properties" I guess. in getProperty you do not specify the property file. Commented Aug 22, 2014 at 5:50

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.