gpt4 book ai didi

java - 新安装时出现 nullpointerException 错误

转载 作者:行者123 更新时间:2023-12-02 12:46:18 25 4
gpt4 key购买 nike

由于这个错误,已经创建了全新的 Eclipse 安装(我删除了所有文件,但我猜我错过了一些文件)

这是错误:

java.lang.NullPointerException
at testing.testsrc.testcase1(testsrc.java:16)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

在一个全新的项目中也得到它,所以相信这远离任何项目特定的配置。

有人可以帮忙吗?

编辑:作为示例,此代码也会返回相同的错误:

package testing;
package TestsStudies;

import static org.junit.Assert.*;

import java.awt.Desktop.Action;
import java.awt.List;
import java.awt.RenderingHints.Key;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.gargoylesoftware.htmlunit.javascript.host.Iterator;

public class TestingStd {

WebDriver driver;
WebElement sidearrow;
WebElement sidemenu;
WebDriverWait wait = new WebDriverWait(driver, 3);

@Before
public void setUp() throws InterruptedException { //Creating driver and connecting to url
String baseURL = "url";
System.setProperty("webdriver.gecko.driver", "/Users/user/Desktop/gecko/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(baseURL);
driver.findElement(By.id("isid")).sendKeys("username");
WebElement Password = driver.findElement(By.id("password"));
Password.sendKeys("password");
Password.sendKeys(Keys.RETURN);
Thread.sleep(1500);
driver.findElement(By.xpath("//img[@ src='/url/images/icons/logo_gif/Br.svg']")).click();;
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"nosaveq\"]/div[3]/div[1]/span/img")).click();;
Thread.sleep(1500);

}

@Test
public void expDescriptionandSave() throws InterruptedException {

sidemenu = driver.findElement(By.id("optionstd"));
sidearrow = driver.findElement(By.id("slideButton_internal"));
Actions builder = new Actions(driver);
builder.moveToElement(sidemenu).perform();
Thread.sleep(1500);
builder.moveToElement(sidearrow).click().perform();
driver.findElement(By.xpath("//*[@id=\"unusedFolderProtocols\"]/div[2]")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[@id=\"unusedFolderProtocolsContent\"]/div[1]/div[2]/div/img")).click(); //click BioELN
Thread.sleep(1500);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
System.out.println(timeStamp + " experimentDescriptionandSave");
driver.switchTo().frame("details");
driver.findElement(By.cssSelector("#experiments > table > tbody > tr:nth-child(3) > td:nth-child(2) > textarea")).sendKeys(timeStamp);
driver.findElement(By.xpath("//*[@title='create study']")).click();
Thread.sleep(1500);
String timeStampSecond = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
System.out.println(timeStampSecond + " experimentDescriptionandSave");
WebElement description = driver.findElement(By.name("DESCR"));
description.clear();
description.sendKeys(timeStampSecond);
driver.findElement(By.xpath("//*[@id=\"exp\"]/table/tbody/tr[11]/td/label[1]")).click();
Thread.sleep(1500);
String descriptionText = driver.findElement(By.name("DESCR")).getText();
assertTrue(descriptionText.contains(timeStampSecond));


}

最佳答案

根据您的代码,您没有初始化驱动程序变量。基本代码是 -

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

此外,根据您要使用的浏览器,您还需要下载并安装相应的驱动程序。对于 Firefox,它称为 GeckoDriver;对于 Chrome,它称为 ChromeDriver。最后,您需要在脚本中提供驱动程序位置。所以代码看起来像这样 -

System.setProperty("webdriver.gecko.driver", "C:\\some_folder\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

如果您正在寻找详细步骤,那么您可以按照这篇文章 - Selenium WebDriver Setup

关于java - 新安装时出现 nullpointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com