简体   繁体   中英

java.lang.NullPointerException while using Actions Class

I'm beginner person in automation testing. Currently I try to writing test with Actions class. ChromeDriver 2.42 version Selenium 3.14 version 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

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).

Your main issue is you have the initialized the object action once globally and again within UruchamianieAplikacji() method. The action object declared within UruchamianieAplikacji() method is having the scope/visibility within this method only.

Moving forward you are trying to access the object action from TestCwiczeniowyZKlasaActions() method where the object action have no visibility.

Hence you see 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:

     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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM