使用XML配置和注释初始化JavaBeans类时出现空指针异常

使用XML配置和注释初始化JavaBeans类时出现空指针异常,java,spring,spring-mvc,javabeans,xml-configuration,Java,Spring,Spring Mvc,Javabeans,Xml Configuration,我在尝试初始化JavaBeans类时遇到了一个问题。 我得到一个空指针异常错误 java.lang.NullPointerException at PojoUtilization.verify(PojoUtilization.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Native

我在尝试初始化JavaBeans类时遇到了一个问题。 我得到一个空指针异常错误

java.lang.NullPointerException
    at PojoUtilization.verify(PojoUtilization.java:29)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
下面是代码片段

我将bean xml作为loginda.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean id="gmailLogin" class="PojoExample">
    <property name="userName" value="abcd@gmail.com"/>
    <property name="password" value="xyz"/>
</bean>
`

我正试图通过以下代码初始化bean类pojoutilization

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;


@ContextConfiguration({"classpath:LoginData.xml"})
public class PojoUtilization {

    @Autowired
    PojoExample gmailLogin;

@Test
    public void verify() {
        System.setProperty("webdriver.chrome.driver", "C://Users//E004146//Downloads//workspace/chromedriver.exe");
        WebDriver obj = new ChromeDriver();
        obj.manage().window().maximize();
        obj.get("https://www.google.com/gmail/about/#");
        System.out.println("Clicked on sign in button");
        obj.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebElement signIn = obj.findElement(By.xpath("//a[contains(text(),'Sign In')]"));
        signIn.click();
        WebElement userName = obj.findElement(By.id("identifierId"));
        userName.sendKeys(gmailLogin.getUserName());
        obj.close();
    }
}
当我试图执行代码时,我得到了一个错误作为空指针异常

java.lang.NullPointerException
    at PojoUtilization.verify(PojoUtilization.java:29)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
尝试在loginda.xml文件中添加

尝试在示例中添加构造函数。
没有参数和两个参数。

我通过继承AbstractTestNGSpringContextTests类解决了这个问题

public class PojoUtilization extends AbstractTestNGSpringContextTests  {
}
通过继承它,我能够初始化Bean类

 result = "abcd@gmail.com"

很抱歉,因为我无法在Somehow中添加答案,我的意思是在定义bean之前尝试添加LoginData.xml文件。当我添加时,出现了一个错误,即注释配置未绑定到此处。我尝试使用默认构造函数,但仍然出现了相同的错误。不知怎的,它无法从bean中获取值。创建带有@Before注释的方法。还有人知道上面的代码段有什么问题吗?我假设第29行是userName.sendKeysgmailLogin.getUsername;这个电话将有助于在将来补充这一点。我不熟悉TestNG,但我猜您还没有设置测试来了解Spring并自动连接gmailLogin字段。您是否调试过以检查它是否为空?嘿,Darren,我调试了它,发现如下字符串value=gmailogin.getUserName;gmailLogin:null值为null。是的,这很酷,即使它通过继承AbstractTestNGSpringContextTests类对我有效。谢谢