0

This is the error I am getting

FAILED CONFIGURATION: @BeforeTest beforetest 
java.lang.NullPointerException

This is my Config reader class to load my property file

public  static void Configreader() {
    try {
        File src = new File("./src/test/resources/config.properties");
        FileInputStream fis = new  FileInputStream(src);
        pro = new Properties();
        pro.load(fis);
        System.out.println("Property class loaded");
    } catch (Exception e) {
        System.out.println("Exception is" +e.getMessage());
    }
}

This is my test class where i want to access my webelements

public class LoanDetails extends Configreader {

    static Properties pro;
    WebDriver driver;

    @BeforeTest
    public  void beforetest() throws Exception { 
        Configreader();
        driver = Browser.GetBrowser();
        System.out.println(" value  is " +pro.getProperty("account_xpath"));
    }
}

i need to access my webelement ("account_xpath") otherwise everything is working

i have attached my property file below where i need to acess my webelement (account_xpath)

enter image description here

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
jino
  • 69
  • 1
  • 12

2 Answers2

1

You are getting Null Pointer Exception because

  1. You have created a new Properties class reference in LoanDetails class again and used it.

  2. Your Configreader method is never called as there is no testng Annotation in Configreader class so testng will skip that.

Solution -

  1. Make Configreader method as static
  2. Make Properties class obj ref as static
  3. Call Configreader method in your before test
  4. Property value will work without NullPointerException

Try below code-

package com.example;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Configreader {

    // make property as static
    public static Properties pro;

    // make method as static
    public static void ConfigFileReader() 
      {
             try {
                  File src = new File("./src/test/resources/config.properties");
                  FileInputStream fis = new  FileInputStream(src);
                  pro = new Properties();
                  pro.load(fis);
                  System.out.println("Property class loaded");
              } 
              catch (Exception e) {
                  System.out.println("Exception is" +e.getMessage());
              }
      }
}

package com.example;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class LoanDetails extends Configreader {

    @BeforeTest
    public  void beforetest() throws Exception { 
       // Called this method in before test annotation method  
        ConfigFileReader();    
        //  driver = Browser.GetBrowser();
        System.out.println(pro.getProperty("account_path"));
                driver.findElement(By.xpath(pro.getProperty("account_xpath"))).click();
    }

    @Test
    void testmain() {
        System.out.println("Testng test");
    }
}

enter image description here

Amit Jain
  • 4,389
  • 2
  • 18
  • 21
  • Could u plz hv a look – jino Aug 25 '18 at 02:09
  • what error you are getting? I have tested this code it was working. – Amit Jain Aug 25 '18 at 02:18
  • In your example you have used static Properties pro; in LoanDetails class it should be in Configreader class only. Derived class with use static variable of Parent class... Make sure you have @Test method in LoanDetails class else testng will skip Before method annotation – Amit Jain Aug 25 '18 at 02:30
  • @jino - Please check once again I have updated the code for your reference and also added execution results. – Amit Jain Aug 25 '18 at 08:59
  • can u help me in my another question https://stackoverflow.com/questions/52033851/datadriven-not-working-using-testng-data-provider – jino Aug 28 '18 at 04:57
  • plz have a look – jino Aug 28 '18 at 11:46
0

The reason you are getting this error is due to the fact you have declared your pro variable in your LoanDetails class but not initialized any value. So when you do this -

pro.getProperty("account_xpath")

it will throw NullPointerException since pro is null.

Do one thing, create a get method for account_xpath as well in Configreader class, just like you did for "ChromePath" and "ApplicationURL" and access that method.

Code:

public class Configreader {

  //your rest of the code
  public  String getAccountXPath()
       {
          String account_xpath = pro.getProperty("account_xpath");
          return account_xpath ;
       }
 }

Now, in your LoanDetails class, use it as:

@BeforeTest
public  void beforetest() throws Exception { 

        driver = Browser.GetBrowser();
        driver.findElement(By.xpath(this.getAccountXPath().trim())).click(); 
  }
Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29