-1

I have below code:

public class BasePage {
    public static WebDriver driver;
    public static Properties prop;
    FileInputStream objfile;

    @Test
    public void BasePages() throws IOException {
        try {
            prop = new Properties();

            objfile = new FileInputStream(System.getProperty("app.properties"));
            prop.load(objfile);
            System.out.println("file loaded");
        } catch (Exception e) {
            System.out.println("catch exception" + e);
        }
    }
}

The FileInputStream shows NullPointerException. I tried debugging the above code only after reading this line it moves to the catch block.

Can anyone explain why i'm getting Exception and how to solve this?

app.properties file contains below lines:

baseUrl = "https://www.google.com/";
browser="chrome";
NarendraR
  • 7,577
  • 10
  • 44
  • 82
Madhu
  • 1
  • 2
  • ``` public void Initialization() { String baseUrl = prop.getProperty("baseUrl"); String browser = prop.getProperty("browser"); System.out.println(baseUrl); System.out.println(browser); if(browser.contentEquals("chrome")) { System.setProperty("webdriver.chrome.driver","C:\\Windows\\chromedriver.exe"); driver = new ChromeDriver(); System.out.println("Chrome loaded"); } System.out.println(baseUrl); System.out.println(browser); driver.get(baseUrl); }} ``` – Madhu Mar 30 '20 at 10:37
  • why did u initialized "objfile' 2 time at class level and inside test function ?? – Amit Jain Mar 30 '20 at 10:39
  • It is a typing mistake i edited the code @Amit jain – Madhu Mar 30 '20 at 10:43
  • objfile= new FileInputStream("give full properties file path") – Amit Jain Mar 30 '20 at 11:02
  • @Madhu, If any answer is helpful for you then please do `accept` by click on tick mark below vote count. So it can be helpful for others. Thanks – NarendraR Mar 31 '20 at 10:51

3 Answers3

0

Please try below solution:

   try {

    InputStream  objfile = new FileInputStream("path/to/app.properties")) {

    Properties prop = new Properties();

    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("baseUrl"));
    System.out.println(prop.getProperty("browser"));


} catch (IOException ex) {
    ex.printStackTrace();
}
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
0
objfile= new FileInputStream(System.getProperty("app.properties"));

The above line is causing the issue because System.getProperty("app.properties") returns null. Don't know why you are using this.

Just remove System.getProperty() as shown below. Make sure you are using right file path

objfile = new FileInputStream("app.properties");

If you intended to access current project directory then code should be

objfile = new FileInputStream(System.getProperty("user.dir")+"/"+"app.properties");
NarendraR
  • 7,577
  • 10
  • 44
  • 82
0
  1. give full properties file path objfile= new FileInputStream("give full properties file path")

  2. if 'objfile' which is an object is not created due to wrong path

  3. and we are trying to dereference a null object, hence getting null pointer exception prop.load(objfile);

Note- i had given the answer fastest can be seen in comments section. please consider while accepting, thanks

Amit Jain
  • 4,389
  • 2
  • 18
  • 21