-1

I am trying to implement Page Object Factory in Selenium WebDriver but, when run de code, the Eclipse Show me the following message error:

java.lang.NullPointerException at org.openqa.selenium.support.ui.Select.(Select.java:44) at page.FaturamentoGeTratamentoOsPage.preencherCampoMotivo(FaturamentoGeTratamentoOsPage.java:24) at test.FaturamentoGeConectividadeFacilidadesTest.selecionarFacilidades(FaturamentoGeConectividadeFacilidadesTest.java:38) 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: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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 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:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

The error occurs in line 23 the class FaturamentoGeTratamentoOsPage and in line 38 in class FaturamentoGeConectividadeFacilidadesTest.

Follow the code of 2 classes and image error.

FaturamentoGeTratamentoOsPage

public class FaturamentoGeTratamentoOsPage {

WebDriver driver;

 @FindBy(id = "cboMotivo") 
 WebElement CBOMotivo;

public FaturamentoGeTratamentoOsPage(WebDriver driver) {
    this.driver = driver;
}
public void preencherCampoMotivo(String CampoMotivo) {
    // Campo Motivo
    Select slcMotivo = new Select(CBOMotivo);
    slcMotivo.selectByVisibleText(CampoMotivo); 
}
}

FaturamentoGeConectividadeFacilidadesTest

public class FaturamentoGeConectividadeFacilidadesTest {
static WebDriver driver;
@Before
public void setUp() throws Exception {
    SelecionarNavegador nav = new SelecionarNavegador();
    driver = nav.iniciarNavegador("chrome", "http://10.5.9.45/BkoMais_Selenium/");
}
@Test
public void selecionarFacilidades() throws Exception {
    // Logando na aplicação
    LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
    login.logar("844502", "Bcc201707");

    // BackOffice >> FaturamentoGe >> Conectividade
    FaturamentoGeConectividadeFacilidadesPage menu = new FaturamentoGeConectividadeFacilidadesPage(driver);
    menu.logarFaturamentoGeConectividade();

    //Registro >> Novo caso
    RegistroNovoCasoPage reg = new RegistroNovoCasoPage(driver);
    reg.registrarCaso();

    //Preencher campos
    FaturamentoGeTratamentoOsPage campo = new FaturamentoGeTratamentoOsPage(driver);
    campo.preencherCampoMotivo(" Concluido ");
}
@After
public void tearDown() throws Exception {
    Thread.sleep(5000);
    driver.quit();
}
}

FaturamentoGeTratamentoOsPage

FaturamentoGeConectividadeFacilidadesTest

Cris Luengo
  • 43,236
  • 6
  • 46
  • 92
  • Add PageFactory.initElements(driver, this); this to ur object class constructure – Ankur Singh Jan 12 '18 at 18:39
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – JeffC Jan 12 '18 at 21:11
  • Possible duplicate of [NullpointerException in Selenium when using SendKeys](https://stackoverflow.com/questions/44968344/nullpointerexception-in-selenium-when-using-sendkeys) – DebanjanB Jan 14 '18 at 20:23

1 Answers1

0

This is happening because you are using PageFactory provided by JAVA however, not initializing it.

There are two places you can initialize it,

Inside Page Object

public FaturamentoGeTratamentoOsPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
}

While Create Page Object

FaturamentoGeTratamentoOsPage campo = PageFactory.initElements(driver, FaturamentoGeTratamentoOsPage.class);
Gaurang Shah
  • 8,589
  • 3
  • 45
  • 90