-1

i am getting java.lang.NullPointerException error at following line in both files Base file and HomePage file at following line code

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); and driver.get("https://www.google.com");

Base class public class Base {

    public WebDriver driver ;

    public WebDriver InitializeDriver() throws IOException
    {
        Properties Prop =new Properties();

        FileInputStream fis=new FileInputStream("C:\\Users\\Raj\\eclipse-workspace\\MavenSelenium\\src\\main\\java\\FirstMaven\\MavenSelenium\\Data.properties");

        Prop.load(fis);
        String browsername = Prop.getProperty("browser");

        if (browsername=="Chrome")
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Raj\\eclipse-workspace\\Work\\chromedriver.exe");
            driver=new ChromeDriver();
        }
        else if (browsername=="Firefox")
        {
           System.setProperty("webdriver.chrome.driver", "C:\\Users\\Raj\\eclipse-workspace\\Work\\geckodriver.exe");
           driver=new FirefoxDriver();  
        }
        else if(browsername=="IE")
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Raj\\eclipse-workspace\\Work\\IEDriverServer.exe");
            driver=new InternetExplorerDriver();
        }
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }

}

and HomePAge Class

public class HomePage extends Base{

@Test
public void Navigation() throws IOException
{

driver=InitializeDriver();
driver.get("https://www.google.com");

}   

}

  • 1
    Well if you compared strings correctly, then driver wouldn't be null. – Roddy of the Frozen Peas May 16 '18 at 03:14
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  May 16 '18 at 03:20
  • yes string compered correctly. in debugger it is throwing error at driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); – user3059218 May 16 '18 at 03:43

1 Answers1

-1

A stacktrace is always helpful here but I was able to figure out what was wrong.

Based on the code you are getting a NPE because driver is never set. This is because browsername=="Chrome" should be browsername.equals("Chrome") or "Chrome".equals(browsername).

== compares the reference of the object where as .equals compares equality.

Jimeh
  • 367
  • 1
  • 6
  • 16
  • Thanks but .equals wont work in if condition – user3059218 May 16 '18 at 03:25
  • `if (browsername.equals("Chrome"))` most definitely does work in if conditions. – Jimeh May 16 '18 at 03:27
  • Thanks lot , i added it run as it is but issue is still there it is throwing error at driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); – user3059218 May 16 '18 at 03:38
  • make sure you add it to all of your if statements. Also make sure your browser property matches completely (including case). It might be worth changing the if statement to `browsername.equalsIgnoreCase("Chrome")` – Jimeh May 16 '18 at 03:54
  • If you are still having issues, please show me what the "browser" property is in your Data.properties file. – Jimeh May 16 '18 at 03:58
  • yes I did it at all and here is the properties file data : browser=Chrome and following error am getting – user3059218 May 16 '18 at 04:33
  • FAILED: Navigation java.lang.NullPointerException at SecMaven.Panda.Base.InitializeDriver(Base.java:41) at SecMaven.Panda.HomePage.Navigation(HomePage.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) – user3059218 May 16 '18 at 04:33
  • 1
    In addition to using `.equals`, I would recommend adding an `else` block to the driver selection to see if the problem is not in configuration: `else { throw new RuntimeException("Configured driver not available: '" + browsername + "'"); }` – Edvins May 16 '18 at 07:38