-2

I'm writing a Java code that would run a simple automation scenario in Chrome or Firefox - depending on the user's input. It starts running (opens a browser), but then throws java.lang.NullPointerException. I thought then the null I assigned the driver variable would later be overridden, but it isn't. How can this be fixed? Thanks!

package com.selenium;

import java.util.Scanner;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        // environment variable
        System.setProperty("webdriver.chrome.driver", "C:\\Automation\\libs\\Drivers\\chromedriver.exe");
        //WebDriver chromeDriver = new ChromeDriver();
        System.setProperty("webdriver.gecko.driver", "C:\\Automation\\libs\\Drivers\\geckodriver.exe");
        WebDriver driver = null;

        Scanner scanner = new Scanner(System.in);
        int option = scanner.nextInt();
        System.out.println("Please enter 1 for Chrome or 2 for Firefox " + option);
        if (option == 1)
        {
            WebDriver driver1= new FirefoxDriver();
        }
        else if 
        (option == 2)
        {
            WebDriver driver2 = new ChromeDriver();
        }
        else 
            System.out.println("Please enter a correct number " + option);

        String baseURL = "https://login.salesforce.com/?locale=eu";

        driver.get(baseURL);

        WebElement userName = driver.findElement(By.id("username"));
        userName.sendKeys("Yan");

        WebElement password = driver.findElement(By.id("password"));
        password.sendKeys("123456");

        WebElement rememberCheckbox = driver.findElement(By.id("rememberUn"));
        rememberCheckbox.click();

        WebElement bLogin = driver.findElement(By.id("Login"));
        bLogin.click();

        }

    }
Yan
  • 83
  • 4
  • 3
    Can you show stacktrace – sForSujit Aug 03 '17 at 11:10
  • False: WebDriver driver1= new FirefoxDriver(); Right: driver = newFireFoxDriver(), – tomas Aug 03 '17 at 11:10
  • 3
    You are not setting the variable `driver` to anything else than `null`. How did you expect that it would be "overridden"? Note that you are creating new variables `driver1` and `driver2`, but these of course do not have any effect on the variable `driver`. – Jesper Aug 03 '17 at 11:12
  • You just create new drivers inside your if{}. Just assign like if (option == 1) { driver1= new FirefoxDriver(); } – Supun Amarasinghe Aug 03 '17 at 11:12

4 Answers4

4

driver is never assigned, you just create new drivers. Change:

    if (option == 1)
    {
        WebDriver driver1= new FirefoxDriver();
    }
    else if 
    (option == 2)
    {
        WebDriver driver2 = new ChromeDriver();
    }

to:

    if (option == 1)
    {
        driver = new FirefoxDriver();
    }
    else if 
    (option == 2)
    {
        driver = new ChromeDriver();
    }
Johan
  • 861
  • 8
  • 24
0

I guess driver.get(baseURL); throws. In bodies of your conditional statements you are creating new variables (driver1, driver2) and never use them. Please assign value to previously declared driver variable.

if (option == 1)
{
   WebDriver driver1= new FirefoxDriver();
}

Would become

if (option == 1)
{
   driver = new FirefoxDriver();
}

And

else if (option == 2)
{
   WebDriver driver2 = new ChromeDriver();
}

Would become

else if (option == 2)
{
   driver = new ChromeDriver();
}
Andrius
  • 1,677
  • 2
  • 11
  • 14
0

Look at the line driver.get(baseURL)

You call a method on this Object, but this object is still null at this point.

That's why the NullPointerException is occuring.

T.Semmler
  • 209
  • 1
  • 12
0

Here is the Answer to your Question:

You have to take care of a lot of facts in your code as follows:

  1. Keep the configuration of environment variable as in System.setProperty with in particular for() loops.

  2. Keep the line scanner.nextInt() after printing the text Please enter 1 for Chrome or 2 for Firefox.

  3. Once you complete using the scanner instance close it to prevent future Resource Leakage.

  4. Keep the name of the WebDriver instance as driver throughout your Automation Framework scope.

  5. In your code, you have declared WebDriver instance as null in the beginning and moving forward you were trying either to initiate driver1 or driver2 again. Hence you faced java.lang.NullPointerException
  6. Here is the sample code block as per your requirement which will use Mozilla Firefox, browse to url https://login.salesforce.com/, provide username & password and finally click on Log In button:

    package demo;
    
    import java.util.Scanner;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Q45482474 {
    
        public static void main(String[] args) {
    
    
        WebDriver driver = null;
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter 1 for Chrome or 2 for Firefox : ");
        int option = scanner.nextInt();
        scanner.close();
        if (option == 1)
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            driver= new FirefoxDriver();
        }
        else if 
        (option == 2)
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        else 
            System.out.println("Please enter a correct number.");
    
        String baseURL = "https://login.salesforce.com/?locale=eu";
    
        driver.get(baseURL);
    
        WebElement userName = driver.findElement(By.id("username"));
        userName.sendKeys("Yan");
    
        WebElement password = driver.findElement(By.id("password"));
        password.sendKeys("123456");
    
        WebElement rememberCheckbox = driver.findElement(By.id("rememberUn"));
        rememberCheckbox.click();
    
        WebElement bLogin = driver.findElement(By.id("Login"));
        bLogin.click();
    
        }
    
    }
    
  7. On executing this program you will observe the following output on the console:

    Please enter 1 for Chrome or 2 for Firefox : 
    1
    1501764400779   geckodriver INFO    geckodriver 0.18.0
    1501764400792   geckodriver INFO    Listening on 127.0.0.1:35604
    1501764401293   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
    1501764421881   Marionette  INFO    Listening on port 48303
    Aug 03, 2017 6:17:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    

Let me know if this Answers your Question.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217