TestNG:@Test 方法中的 java.lang.NullPointerException

TestNG: java.lang.NullPointerException in the @Test Method

请帮助我,@BeforeClass 运行s 成功,但进入 @Test 方法会抛出以下异常。 `@Test' 中的代码没有 运行 正确。

我能够 运行 几个月前成功地编写了代码,但现在它不起作用了。我正在使用 selenium-server-standalone-2.39.0.jar.

异常:

java.lang.NullPointerException
    at scripts.IRS_TP_3_Contribution.test990TP2(IRS_TP_3_Contribution.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

代码如下:

@BeforeClass 

public void launchBrowser() throws IOException, InterruptedException 
{
System.setProperty("webdriver.chrome.driver","E://FELIX//Automation Testing//Jar files//chromedriver.exe");  
        WebDriver driver = new ChromeDriver();      
        driver.get("Link Hided");

        driver.findElement(By.id("LEmailAddress")).clear();
        driver.findElement(By.id("LEmailAddress")).sendKeys("felix@gmail.com");
        driver.findElement(By.id("Password")).clear();
        driver.findElement(By.id("Password")).sendKeys("123456");
        driver.findElement(By.id("btnSubmit")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//body/div[2]/div[3]/div[3]/div/div[1]/table/tbody/tr/td[1]/form/div/div[3]/table/tbody/tr[9]/td[3]/a")).click();
        //driver.findElement(By.xpath("(//a[contains(text(),'Edit')])[5]")).click();
        driver.findElement(By.id("btnContributionRevenueStart")).click();
        driver.findElement(By.id("btnContributionGiftsStart")).click();
        driver.findElement(By.linkText("Add Contributions and Grants")).click();
        //driver.findElement(By.id("ContributorName")).clear();


        // Need Iteration for 992
        //driver.findElement(By.id("Line1_IsGrantsEquivalentYes")).click();
        //driver.findElement(By.xpath("//input[@value='Go to Express990']")).click();
        /*for (int second = 0;; second++) {
            if (second >= 20) fail("timeout");
            try { if (isElementPresent(By.xpath("//input[@value='Go to Express990']"))) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }

        driver.findElement(By.xpath("//input[@value='Go to Express990']")).click();*/
    }

@Test (dataProvider = "DP1")
      public void test990TP2(String contributorName, String contributorType, String add1, String city, String state, String zip, String amount) throws Exception {
        driver.manage().timeouts().implicitlyWait(05, TimeUnit.SECONDS);
        //driver.findElement(By.id("btnAddAnother")).click();
        // Orgnaization Info
        // ERROR: Caught exception [ERROR: Unsupported command [typeKeys | id=EIN | 00-9000004]]
        /*driver.findElement(By.linkText("Add Contributions and Grants")).click();
        //driver.findElement(By.id("ContributorName")).clear();
        driver.findElement(By.id("ContributorName")).sendKeys(contributorName);*/
        driver.findElement(By.id("ContributorName")).sendKeys(contributorName);
        new Select(driver.findElement(By.id("ContributorType"))).selectByVisibleText(contributorType);
        driver.findElement(By.id("Address1")).clear();
        driver.findElement(By.id("Address1")).sendKeys(add1);
        driver.findElement(By.id("City")).clear();
        driver.findElement(By.id("City")).sendKeys(city);
        new Select(driver.findElement(By.id("USStates"))).selectByVisibleText(state);
        driver.findElement(By.id("ZipCode")).clear();
        driver.findElement(By.id("ZipCode")).sendKeys(zip);
        driver.findElement(By.xpath("//div[@id='codeRefer']/table/tbody/tr[16]/td[2]/p/label/span")).click();
        driver.findElement(By.id("IsContributionNonCashYes")).click();
        driver.findElement(By.id("GrantTotalContribution")).clear();
        driver.findElement(By.id("GrantTotalContribution")).sendKeys(amount);
        driver.findElement(By.id("btnAddAnother")).click();
        // Ends
      }

你得到 NullPointerException 因为你的指针 - driver 是在方法 launchBrowser() 中创建的。

如果你想在这两种方法中使用指针driver,你必须将它设置为实例变量。

示例:

public class Example {
    Webdriver driver; //instance variable - available for all methods in this class

@BeforeTest
public void launchBrowser() {
    driver = new Chromedriver();
    }    
}

希望对您有所帮助!