1

I am facing an issue while trying to initialize the java beans class. I am getting an error of null pointer exception.

Below is the code snippets.

I have beans xml as 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>

`

And bean class as `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;
}


}

`

And I am trying to initialize the bean class(PojoUtilization)through the following code

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

When I try to execute the code I am getting an error as the null pointer exception.

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)
khan
  • 547
  • 3
  • 18
  • Folks anyone has any idea whats wrong with the above code snippet? – khan Jul 20 '18 at 13:58
  • I assume that line 29 is the the `userName.sendKeys(gmailLogin.getUsername());` call (would be helpful to add that in future). I'm not familar with TestNG, but I would guess you haven't set the test up to be aware of Spring and actually autowire the the `gmailLogin` field. Have you debugged to check it is not null? https://www.mkyong.com/unittest/testng-spring-integration-example/ – Darren Forsythe Jul 21 '18 at 16:48
  • Hey Darren I debugged it and found it as below "String value = gmailLogin.getUserName(); gmailLogin:null" The value is null. – khan Jul 22 '18 at 06:00

3 Answers3

1

I got the issue resolved by inheriting the AbstractTestNGSpringContextTests class.

public class PojoUtilization extends AbstractTestNGSpringContextTests  {
}

By inheriting it I am able to initialize the Bean class.

 result = "abcd@gmail.com"
khan
  • 547
  • 3
  • 18
  • 1
    Yep that's cool,even it worked for me by inheriting the AbstractTestNGSpringContextTests class. Thanks – shaik Jul 23 '18 at 06:09
-1

Try with adding (<context:annotation-config />) in LoginData.xml file

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ritpd113
  • 24
  • 3
-1

Try to add constructor in PojoExample. Without parameter and two parameter.

Rashed
  • 124
  • 12
  • 1
    I did try with the default constructor but stills I got the same error. – khan Jul 20 '18 at 05:38
  • Somehow it's can't get the value from bean. Create a method with @Before annotation. And initialize the value from this method. – Rashed Jul 22 '18 at 12:17