public class TestBase {
public static WebDriver driver = null;
public static Properties prop = null;
public TestBase() {
try {
prop = new Properties();
FileInputStream ip = new FileInputStream("C:\\seleniumFolder\\SampleMavenProject\\src\\main\\java\\com\\crm\\qa\\config\\config.properties");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization() {
String browsername = prop.getProperty("browser");
if (browsername.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\seleniumFolder\\chromedriver.exe ");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
}
Asked
Active
Viewed 496 times
-1

halfer
- 19,824
- 17
- 99
- 186

kavitha kannan
- 1
- 1
-
At which line are you seeing the exception? – undetected Selenium Mar 13 '18 at 12:55
-
Your initialization method should consider the else-case. Right now it runs into a nullpointer-exception if browsername is not equal to "chrome", which is bad. Also, considering https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#getProperty(java.lang.String): browsername can potentially be null, thus your if-check should call equals() on the literal "chrome" instead of your variable browsername. – Mercious Mar 13 '18 at 13:44
-
Thank you! I have removed if check and its working fine now. – kavitha kannan Mar 13 '18 at 15:54
1 Answers
0
Seems your FileInputStream ip
is not getting initialized. You have to provide (\\)
instead of (\)
as follows :
FileInputStream ip = new FileInputStream("C:\\seleniumFolder\\SampleMavenProject\\src\\main\\java\com\\crm\\qa\\config\\config.properties");
Update
You are seeing java.lang.NullPointerException
because to execute a Java Program you have to initiate the execution through a main()
method or @Test
annotated method (if using TestNG).
So if you want to execute this program through main()
method you have to put the current code of public static void initialization()
method into public static void main(String[] args)
method and execute the program as a Java Program.
You can find a detailed discussion in How to write Selenium Java Application code in IDE through main() and TestNG

undetected Selenium
- 183,867
- 41
- 278
- 352
-
1That was my fault, the formatter I used to fixed the code broke that – jrtapsell Mar 13 '18 at 12:47