我收到一个“线程异常”主“java.lang.NullPointerException”错误,我不知道如何在这种情况下修复它。该应用程序将通过设置并开始打开Firefox,但随后Firefox将在下一步崩溃。 该程序应该在登录我们的站点时检查Glassfish服务器是否仍在工作。
run:
setUp
testExample
Exception in thread "main" java.lang.NullPointerException
at spidamin.Example.testExample(Spidamin.java:32)
at spidamin.Example.main(Example.java:68)
C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 8 seconds)
程序:
package example;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Example {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.out.println("\nsetUp");
driver = new FirefoxDriver();
baseUrl = "http://example.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testExample() throws Exception {
System.out.println("testExample");
driver.get(baseUrl + "/portal/web/guest"); <------------------- Line 32
driver.findElement(By.name("_58_login")).clear();
driver.findElement(By.name("_58_login")).sendKeys("kyle.hoover@example.net");
driver.findElement(By.id("_58_password")).clear();
driver.findElement(By.id("_58_password")).sendKeys("password here");
driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
driver.findElement(By.linkText("You are signed in as"));
driver.findElement(By.linkText("Sign Out")).click();
}
@After
public void tearDown() throws Exception {
System.out.println("tearDown");
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
Process i = Runtime.getRuntime().exec("cmd /c start \"C:\\Users\\user\\Desktop\\Tests\\Test.bat\"");
}
}
public boolean isElementPresent() throws IOException {
System.out.println("isElementPresent");
try {
System.out.println("True");
driver.findElement(By.linkText("You are signed in as"));
return true;
} catch (NoSuchElementException e) {
System.out.println("false");
Process i = Runtime.getRuntime().exec("cmd /c start \"C:\\Users\\user\\Desktop\\Tests\\Test.bat\"");
return false;
}
}
public static void main(String[] args) throws Exception {
new Example().setUp();
new Example().testExample(); <------------------- Line 68
new Example().isElementPresent();
new Example().tearDown();
System.out.println("\nprogram finished");
}
}
答案 0 :(得分:1)
new Example().setUp();
new Example().testExample(); <------------------- Line 68
在第一个语句中,您创建一个新的Example
对象并调用setUp()
。
在第二个声明中,您创建 第二 新Example
并致电testExample()
。在第二个对象中,setUp()
尚未被调用,因此driver
仍然为空,从而导致异常。
你想做的是
Example ex = new Example();
ex.setUp();
ex.testExample();
ex.isElementPresent();
ex.tearDown();