【问题标题】:Null Pointer Exception while initializing the java beans class using XML configuration and annotations使用 XML 配置和注释初始化 java bean 类时出现空指针异常
【发布时间】:2018-07-19 17:22:38
【问题描述】:

我在尝试初始化 java bean 类时遇到问题。 我收到空指针异常的错误。

下面是代码sn-ps。

我有 bean xml 作为 LoginData.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 类为 `PojoExample

public class PojoExample {
private String userName;
private String password;



public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}


public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}


}

`

我正在尝试通过以下代码初始化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)

【问题讨论】:

  • 大家知道上面的代码 sn-p 有什么问题吗?
  • 我假设第 29 行是 userName.sendKeys(gmailLogin.getUsername()); 调用(将来添加它会有所帮助)。我对 TestNG 不熟悉,但我猜您还没有设置测试以了解 Spring 并实际上自动装配 gmailLogin 字段。您是否已调试以检查它不为空? mkyong.com/unittest/testng-spring-integration-example
  • 嘿Darren 我调试了一下,发现如下“String value = gmailLogin.getUserName(); gmailLogin:null” 值为null。

标签: java spring spring-mvc javabeans xml-configuration


【解决方案1】:

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

public class PojoUtilization extends AbstractTestNGSpringContextTests  {
}

通过继承它,我可以初始化 Bean 类。

 result = "abcd@gmail.com"

【讨论】:

  • 是的,这很酷,即使它通过继承 AbstractTestNGSpringContextTests 类对我有用。谢谢
【解决方案2】:

尝试在PojoExample中添加构造函数。 无参数和两个参数。

【讨论】:

  • 我确实尝试过使用默认构造函数,但仍然遇到同样的错误。
  • 不知何故无法从 bean 中获取值。使用 @Before 注释创建一个方法。并通过该方法初始化值。
【解决方案3】:

尝试在 LoginData.xml 文件中添加(&lt;context:annotation-config /&gt;)

【讨论】:

  • 抱歉,我无法在 () 内的答案中添加
  • 我的意思是在定义 bean 之前尝试在 LoginData.xml 文件中添加
  • 当我添加时,我收到一个错误,因为“Annotation-config”未在此处绑定。
猜你喜欢
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
  • 2019-10-27
  • 2013-04-08
  • 1970-01-01
相关资源
最近更新 更多