1

Tried to create properties file and retrieving information from it , but am getting an java.lang.NullPointerException ,also tried try and catch , as am very new to coding, can anyone please let me know why am getting null pointer Exception.

package ObjectRepository;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class PropertiesGuru99Bank {
    WebDriver driver;
      @Test
  public void f()  {
      File file=new File("E:\\selenium\\Rahul\\Project\\src\\ObjectRepository\\object.properties");
      FileInputStream f=null; 
      try {
          f=new FileInputStream(file);
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.getStackTrace());
    }
      Properties prop=new Properties();
        try {
             prop.load(f);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getStackTrace());
        }
     driver.get(prop.getProperty("url"));
      driver.findElement(By.id(prop.getProperty("id"))).sendKeys("rahul");
  }
  @BeforeTest
  public void beforeTest() {
    System.setProperty("webdriver.chrome.driver", "E:\\selenium\\lib\\chromedriver_win32\\chromedriver.exe");
    new ChromeDriver();   
  }
  @AfterTest
  public void afterTest() {
  }
}

1 Answers1

3

Small typo probably. You are not instantiating the driver. new ChromeDriver(); does not instantite the driver. do driver = new ChromeDriver(); or shown below after setting the driver exe to system property.

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class PropertiesGuru99Bank {
    WebDriver driver;

    @Test
    public void f()  {
        File file=new File("E:\\selenium\\Rahul\\Project\\src\\ObjectRepository\\object.properties");
        FileInputStream f=null; 
        try {
            f=new FileInputStream(file);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getStackTrace());
        }

        Properties prop=new Properties();

        try {
            prop.load(f);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getStackTrace());
        }

        driver.get(prop.getProperty("url"));
        driver.findElement(By.id(prop.getProperty("id"))).sendKeys("rahul");
    }

    @BeforeTest
    public void beforeTest() {
    System.setProperty("webdriver.chrome.driver", "E:\\selenium\\lib\\chromedriver_win32\\chromedriver.exe");
     //this is what you are missing
     driver = new ChromeDriver();
    }

    @AfterTest
    public void afterTest() {
    }
}
Saifur
  • 16,081
  • 6
  • 49
  • 73