有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我正在学习java。当我在cucumber中执行runner类时发生lang.NullPointerException

我创建了一个功能文件,然后创建了selenium基类,在这里我创建了一个从配置文件读取数据的方法,然后在StepDefinition类中给出了驱动程序的详细信息。然后创建runner类并执行它。正在selenium基类中获取空指针异常。你能帮我解决这个问题吗

//Selenium Base class
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.testng.annotations.Test;

public class SeleniumBaseclass
{

    Properties prop;
 public void Property()
    {

        try
        {
            File f= new File("C:\\Users\\watareuman9\\workspace\\Cucumberproject\\src\\test\\resources\\data\\config.property");

            FileInputStream fis = new FileInputStream(f);
             prop=new Properties();

                    prop.load(fis);
                } catch (Exception e) {

                    e.printStackTrace();
                }
        }



        public String getChromepath()
        {
            return prop.getProperty("ChromePath");
        }

        public String getAppurl()
        {
            return prop.getProperty("URL");
        }

    }


//stepdefination file:

    package Stepdfinations;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;

    import cucumber.api.java.en.Given;

    public class Accountstepdefinations  {

        @Given("^I navigated to the Login page$")
        public void i_navigated_to_the_Login_page() 
        {
            SeleniumBaseclass b1=new SeleniumBaseclass();
            System.setProperty("webdriver.chrome.driver", b1.getChromepath());
            WebDriver driver=new ChromeDriver();
            driver.get(b1.getAppurl());

        }

        //@When("^I click on the New Account link$")
        //public void i_click_on_the_New_Account_link() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
           // throw new PendingException();


        //@Then("^I should see the New Account registration page$")
        //public void i_should_see_the_New_Account_registration_page() throws Throwable {
            // Write code here that turns the phrase above into concrete actions
        //    throw new PendingException();
        }

    //
    //}

//Runner clasas:

package Stepdfinations;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources/features")



public class Runner 
{


}



//Error appearing as null exception in the following method:

public String getChromepath()
        {
            return prop.getProperty("ChromePath");
        }

共 (1) 个答案

  1. # 1 楼答案

    在调用SeleniumBaseclass.property()之前,您正在调用SeleniumBaseclass.getChromepath()

    所以,当调用SeleniumBaseclass.getChromepath()prop的值是null。因此,出现了NullpointerException