My Java Code Throws java.lang.NullPointerException : <p>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!</p>

<pre><code>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();

        }

    }
</code></pre>
