0

enter image description here

Have below Selenium Program using Object Repository Library File but the same is failing and shows java.lang.NullPointerException. Have commented the specific syntax lines as "error". Also attached the error screenshot and config.property files.enter image description here. Have tried to identify the cause but could not. please clarify the cause of the failing and error message. enter image description here

package objectrepository;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import objectrepository.Lib_ChromeDriver;

public class OBRClass_2 {

@Test
public void launch_chrome() throws Exception{

Lib_ChromeDriver LCD = new Lib_ChromeDriver();// Lib_ChromeDriver is the Object Repository Library File 

System.setProperty("webdriver.chrome.driver", LCD.Path());// error shown

WebDriver Snap = new ChromeDriver();

Snap.get(LCD.AppURL());


}
}

and:

// a Java Class containing Constructor & method and is saved as Library file.
// Constructor has will call the ObjectRepository File and 
// method will call the Call the ChromeDriver Location to Launch the same
// this Library Class can be called in Selenium program to launch the Chrome Driver 

package objectrepository;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.testng.annotations.Test;

public class Lib_ChromeDriver {

Properties Prop;

@Test
public void Lib_ChromeDriver() throws Exception{// Constructor calling the Object Repository File

File OB_File = new File("./Config/Config.property");
FileInputStream OB_FIS = new FileInputStream(OB_File);
Prop = new Properties();
Prop.load(OB_FIS);
}

public String Path() throws Exception{// method calling the ChromeDriver Path to Launch the same

String ChromePath = Prop.getProperty("ChromeDriver"); // error shown
return ChromePath;
}

public String AppURL() throws Exception{

String AppURL = Prop.getProperty("URL");
return AppURL;
}
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
badri
  • 31
  • 8
  • `Prop` is `null`. Are you expecting it not to be `null`? – nitind Aug 10 '17 at 02:47
  • @badri - does my answer address your problem? – Scary Wombat Aug 10 '17 at 04:03
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Aug 10 '17 at 04:05

1 Answers1

0

change

public void Lib_ChromeDriver()   // not a constructor

to

public Lib_ChromeDriver()       // is a constructor

the code you are calling is not in your constructor but in a method called Lib_ChromeDriver

In this case your code is calling the default constructor which does not have any code in it - therefore the variable Props is never set

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64