简体   繁体   English

使用 Actions 类时的 java.lang.NullPointerException

[英]java.lang.NullPointerException while using Actions Class

I'm beginner person in automation testing.我是自动化测试的初学者。 Currently I try to writing test with Actions class.目前我尝试使用 Actions 类编写测试。 ChromeDriver 2.42 version Selenium 3.14 version Chrome 9.0.3497.100 ChromeDriver 2.42 版 Selenium 3.14 版 Chrome 9.0.3497.100

Below I put my code:下面我把我的代码:

public class KlasaDoCwiczen {
WebDriver driver;
Actions action;
ChromeOptions options;

@Before
public void UruchamianieAplikacji () {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\smartyn\\Desktop\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    Actions action = new Actions(driver);
    driver.get("http://localhost:4200/dashboard");
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
    //driver.quit();
}

@Test
public void TestCwiczeniowyZKlasaActions () {
    action.clickAndHold(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn")))
    .moveToElement(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn")))
    .build()
    .perform();

}
}

So when I execute this I have statement:所以当我执行这个我有声明:

wrz 25, 2018 10:42:16 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS wrz 25, 2018 10:42:16 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到方言:OSS

java.lang.NullPointerException
at KlasaDoCwiczen.TestCwiczeniowyZKlasaActions(KlasaDoCwiczen.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

This error message...这个错误信息...

java.lang.NullPointerException

...implies that the NullPointerException was raised at (KlasaDoCwiczen.java:31). ...暗示NullPointerException是在 (KlasaDoCwiczen.java:31) 处引发的。

Your main issue is you have the initialized the object action once globally and again within UruchamianieAplikacji() method.您的主要问题是您在UruchamianieAplikacji()方法中一次又一次地全局初始化了对象操作 The action object declared within UruchamianieAplikacji() method is having the scope/visibility within this method only.UruchamianieAplikacji()方法中声明的动作对象仅在此方法中具有范围/可见性。

Moving forward you are trying to access the object action from TestCwiczeniowyZKlasaActions() method where the object action have no visibility.继续前进,您正在尝试从对象操作不可见的TestCwiczeniowyZKlasaActions()方法访问对象操作

Hence you see java.lang.NullPointerException .因此你会看到java.lang.NullPointerException

Solution解决方案

  • Keep the declaration of action as a global variable just like driver , options , etc. and remove the other declaration from UruchamianieAplikacji() method:动作声明保留为全局变量,就像driveroptions等一样,并从UruchamianieAplikacji()方法中删除其他声明:

     Actions action = new Actions(driver); //remove this line
  • Or initialize Actions action within TestCwiczeniowyZKlasaActions() method only removing it from global scope and TestCwiczeniowyZKlasaActions() method as follows:或者在TestCwiczeniowyZKlasaActions()方法中初始化Actions action ,仅将其从全局范围和TestCwiczeniowyZKlasaActions()方法中删除,如下所示:

     new Actions(driver).clickAndHold(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn"))) .moveToElement(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn"))) .build() .perform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NullPointerException:使用Firebase时 - java.lang.NullPointerException: while using firebase 使用ServletContext时java.lang.NullPointerException - java.lang.NullPointerException while using ServletContext 调用类方法时发生错误java.lang.NullPointerException - Error java.lang.NullPointerException while calling class method 使用class.getClassLoader()。getResourceAsStream时抛出java.lang.NullPointerException - java.lang.NullPointerException throws while using class.getClassLoader().getResourceAsStream 使用ListView简单适配器时出现Java.Lang.NullPointerException - Java.Lang.NullPointerException while using listview simple adapter 使用用户定义的注释时获取java.lang.NullPointerException - Getting java.lang.NullPointerException while using user defined annotation java.lang.NullPointerException:使用 RecyclerView.Widget 时出错 - java.lang.NullPointerException: Error while using RecyclerView.Widget 使用bean类填充Jtextfield时,java.lang.NullPointerException - java.lang.NullPointerException when populating Jtextfield using a bean class 设置时间时java.lang.NullPointerException - java.lang.NullPointerException while setting time 创建DiskFileItem时java.lang.NullPointerException - java.lang.NullPointerException while creating DiskFileItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM