Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初始化基于Java的Spring直接注入导致空指针错误_Java_Spring_Selenium Webdriver_Nullpointerexception_Initialization - Fatal编程技术网

初始化基于Java的Spring直接注入导致空指针错误

初始化基于Java的Spring直接注入导致空指针错误,java,spring,selenium-webdriver,nullpointerexception,initialization,Java,Spring,Selenium Webdriver,Nullpointerexception,Initialization,我知道空指针错误是因为注入没有正确初始化。不太清楚如何修复它 我有三个文件:main(CommonElements.java)、Constructor.java(bean)和Driver.java(其中包含主代码) 尝试将driver.java注入到CommonElements中,当它运行时,即出现空指针异常 下面是每个的代码。任何帮助都将不胜感激 CommonElements.java package PageFactory; import org.junit.Test; import or

我知道空指针错误是因为注入没有正确初始化。不太清楚如何修复它

我有三个文件:main(CommonElements.java)、Constructor.java(bean)和Driver.java(其中包含主代码)

尝试将driver.java注入到CommonElements中,当它运行时,即出现空指针异常

下面是每个的代码。任何帮助都将不胜感激

CommonElements.java

package PageFactory;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class CommonElements {

private Driver web;

    ApplicationContext ctx = new AnnotationConfigApplicationContext(Constructor.class);      
    Driver page = ctx.getBean(Driver.class); 

    @Test
    public void Common(final Driver web)
    {
        this.web = web;
        web.setup();
    }
}
Constructor.java

package PageFactory;

import org.springframework.context.annotation.*;

//constructor 
@Configuration //using java dependency injection
public class Constructor {
    @Bean
    public Driver driver() {
        return new Driver();
    }

}
Driver.java

package PageFactory;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Driver {

    private String baseUrl;

    public void setup() {
        final WebDriver driver;
        System.setProperty("webdriver.chrome.driver", 
        "drivers/chromedriver.exe"); //location of driver
        ChromeOptions options = new ChromeOptions(); 
        driver = new ChromeDriver(options);
        baseUrl = "https://www.google.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

}

编辑:我不确定junit是否能够在您的测试方法中注入驱动程序bean,您可以尝试类似的方法吗

package PageFactory;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class CommonElements {

private Driver web;

    ApplicationContext ctx = new AnnotationConfigApplicationContext(Constructor.class);      
    Driver page = ctx.getBean(Driver.class); 

    @Test
    public void Common()
    {
        page.setup();
    }
}
另外,由于您正在手动初始化spring上下文,您可能不需要我下面建议的内容

旧答案:

我认为,如果希望junit初始化spring,需要将此注释添加到测试类中

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration // i'm not 100% sure this one is needed 
public class CommonElements {
 ...
}

参考资料:

您可以发布堆栈跟踪吗?另外,您是否使用调试器逐步完成了代码?它甚至不会运行调试器。它是这样说的:启动CommentElements遇到了一个问题:在“启动CommonElements(1)”期间发生了一个内部错误。java.lang.NullPointerExceptionhmmm,这似乎也不起作用,感谢您的帮助!啊……我没有getDriver()或setDriver()方法。这可能会有帮助…我注意到您试图让junit在测试方法中注入bean,我从未见过这样使用它,我会尝试删除驱动程序参数并在测试中执行page.setup(),我正在编辑答案以显示代码