从另一个类的类中获取方法,而不会获得NullPointerException

时间:2016-09-13 09:38:26

标签: java selenium

我有一个名为ReqOrderImpCode的类,其中包含以下代码:

`public class ReqOrderImpCode {
    WebDriver driver;
    WebDriverWait wait;
    PrintStream out;
    int maxim = 0;
    boolean bcase = true;
//Constructor
    public ReqOrderImpCode(WebDriver driver, WebDriverWait wait, PrintStream out) throws FileNotFoundException {
        this.driver = driver;
        this.wait = wait;
        this.out = out;
    }
// Start testing, initialize the page
    public void page() {
        driver.get("http://google.com");
        driver.manage().window().maximize();
    }
//Access to methods
    Methods m = new Methods(driver, wait, out);

public void checkLoginAndProceed() throws FileNotFoundException {
        String pageTitle = driver.getTitle();
        if (pageTitle.equals("GAS")) {
            m.sendKeys(xUsername, "ftalexiuc");
            m.sendKeys(xPassword, "1freedom1");
            m.click(xLogin);
        } 
}`

我用来调用上面方法的方法类看起来像这样:`

public class Methods {

    //Initialization
    WebDriver driver;
    WebDriverWait wait;
    PrintStream out;

    //Constructor
    public Methods (WebDriver driver, WebDriverWait wait, PrintStream out) throws FileNotFoundException {
        this.driver = driver;
        this.wait = wait;
        this.out = out;
        }
    //Method for clicking
    public void click(String s){
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
        element.click();
        }

    //Method for SendKeys
    public void sendKeys(String s, String t){
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
        element.sendKeys(t);
        }
}`

问题是第一个类的m.sendKeys()方法返回NullPointerException错误。我应该如何写这个以避免这个错误,我的代码按预期运行?

0 个答案:

没有答案