繁体   English   中英

从Selenium中的Config属性文件读取值时获取空指针异常

[英]Getting Null Pointer Exception While reading value from Config Properties File in Selenium

package com.HybridFramework.testbase;

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestBase {

public WebDriver driver;

 public Properties a1;
 public File f1;
 public FileInputStream file;
public void loadPropertiesFile() throws IOException{
        Properties a1 = new Properties();
        System.out.println(System.getProperty("user.dir"));
        //f1= new File("E:\\Selenium-2017\\HybridFramework\\src\\main\\java\\com\\HybridFramework\\config\\config.properties");
    f1 = new File(System.getProperty("user.dir")+"/src/main/java/com/HybridFramework/config/config.properties");
    System.out.println("lola");
    file= new FileInputStream(f1);
    a1.load(file);

    f1 = new File(System.getProperty("user.dir")+"/src/main/java/com/HybridFramework/config/or.properties");
    file= new FileInputStream(f1);
    a1.load(file);

    }
    public void getPropertiesData(){

    }
public static void main(String[] args) throws IOException   {
    TestBase test = new  TestBase();
    //test.getBrowser("firefox");
    System.out.println("Start");
    test.loadPropertiesFile();
System.out.println(test.a1.getProperty("Username"));
    test.a1.getProperty("testname");
}



 }

以下是我要参考的属性文件。

config.properties文件

Username=Bhanu
Password=password
url=https://www.google.com

or.properties文件

test=test1
testname=login

错误低于

com.HybridFramework.testbase.TestBase.main(TestBase.java:61)上的线程“ main”中的异常java.lang.NullPointerException

您已经声明了Properties类型的实例属性,但从未初始化。 使用a1 = new Properties(); 代替Porperties a1 = new Properties(); 在您的loadPropertiesFile()方法中。

首先使用环境从属性文件获取属性。 然后,可以将其设置为System属性,然后可以从System.getProperty()使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM