我的Java代码抛出java.lang.NullPointerException

时间:2017-08-03 11:08:31

标签: java selenium exception null automation

我正在编写一个Java代码,可以在Chrome或Firefox中运行简单的自动化方案 - 具体取决于用户的输入。它开始运行(打开浏览器),但随后抛出java.lang.NullPointerException。我当时认为我分配了驱动程序变量的null稍后会被覆盖,但事实并非如此。怎么解决这个问题?谢谢!

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

        }

    }

4 个答案:

答案 0 :(得分:4)

永远不会分配

驱动程序,只需创建新驱动程序即可。变化:

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

为:

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

答案 1 :(得分:0)

我猜driver.get(baseURL);会抛出。在条件语句的主体中,您正在创建新变量(driver1,driver2),并且从不使用它们。请为先前声明的driver变量赋值。

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

会变成

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

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

会变成

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

答案 2 :(得分:0)

查看第driver.get(baseURL)

您在此Object上调用方法,但此时此对象仍为null

这就是发生NullPointerException的原因。

答案 3 :(得分:0)

以下是您的问题的答案:

您必须按照以下方式处理代码中的大量事实:

  1. const redis = require('redis'); const redisClient = redis.createClient(); var values ; redisClient.flushall(); redisClient.hmset('Angelica', { "Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0 }); redisClient.hmset('Bill', { "Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0 }); redisClient.hmset('Dan', { "Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 2.0 }); redisClient.hmset('Hailey', { "Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0 }); redisClient.hmset('Jordyn', { "Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 4.0 }); redisClient.hmset('Sam', { "Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0 }); redisClient.hmset('Veronica', { "Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0 }); redisClient.hmset('Chan', { "Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0 }); redisClient.keys('*', (err, responde) => { redisClient.hmget(responde); values=responde.toLocaleString() console.log(responde.toLocaleString()) }) console.log( values)的配置与environment variable中的System.setProperty循环保持一致。

  2. 打印文本for()后保留第scanner.nextInt()行。

  3. 完成扫描仪实例的使用后,请将其关闭以防止将来Please enter 1 for Chrome or 2 for Firefox

  4. Resource Leakage范围内将WebDriver个实例的名称保留为driver

  5. 在您的代码中,您在开始时已将Automation Framework个实例声明为WebDriver,而您正在尝试再次启动nulldriver1。因此,您遇到了 driver2
  6. 以下是根据您的要求使用Mozilla Firefox的示例代码块,浏览到网址java.lang.NullPointerException,提供用户名&密码,最后点击https://login.salesforce.com/按钮:

    Log In
  7. 执行此程序时,您将在控制台上观察到以下输出:

    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();
    
        }
    
    }
    
  8. 如果这回答你的问题,请告诉我。