我的数组只存储值,直到迭代运行。在完成值的存储并尝试获取这些值后,它返回Null值

时间:2017-12-18 18:47:16

标签: java arrays selenium selenium-webdriver

我的数组只存储值直到迭代运行..在完成值的存储并尝试获取这些值后,它返回Null值并尝试了这么多次以及很多方法来获取值但是我没有用尝试...最后我最终得到了堆栈溢出网站。我希望我的等待在这里结束

 public class RegisterUser extends BrowserInit{

    public String[][] values;

    @Test
    public void register(String Filepath) throws Exception{

        //Clicking Login link in WebPage
        WebElement Login=driver.findElement(By.xpath("//a[@class='login']"));
        Parent=driver.getWindowHandle();
        act=new Actions(driver);
        act.moveToElement(Login).perform();

        //moving the cursor to element and clicking on the link
        act.click(Login).perform();
        Thread.sleep(3000);

        //after clicking on the link checking is their any new tab is opened
        Set<String> windows=driver.getWindowHandles();
        for(String Child : windows){

            //if it found switch the control to new tab
            if(!Parent.equals(Child)){
                driver.switchTo().window(Child);    
            }
        }
        ExcelDataClass data=new ExcelDataClass(Filepath);
        //Clicking register in dropdown
        driver.findElement(By.xpath("//a[@class='dropdown-toggle']")).click();
        WebElement register=driver.findElement(By.xpath("//*[@id='Secondary_Navbar-Account']/ul"));
        act.moveToElement(register);
        List<WebElement> Register=register.findElements(By.tagName("li"));
        for(int i=0;i<Register.size();i++){
            String Linkname=Register.get(i).getText();
            if(Linkname.equals("Register")){
                Register.get(i).click();
                break;
            }
        }
        System.out.println("total rows "+data.RowCount);
        for(int row=1;row<data.sheet.getLastRowNum()+1;row++){

            for(int col=0;col<data.sheet.getRow(row).getLastCellNum();col++){
                //this is the for loop where my array stored the values from the sheet using row index and column index
                values=new String[data.sheet.getLastRowNum()+1][data.sheet.getRow(row).getLastCellNum()+1];
                values[row][col]=data.sheet.getRow(row).getCell(col).getStringCellValue();

            }

//在列迭代完成之后我试图从数组中获取值并通过下面的代码存储在字符串中,但是它返回空值...我不知道我在哪里得到错误但是我的数组返回下面的空值索引甚至填充数据

          String First=values[row][0];
            String Last=values[row][1];
            String Company=values[row][2];
            String Email=values[row][3];
            String Password=values[row][4];
            String CnfPassword=values[row][5];
            String Add1=values[row][6];
            String Add2=values[row][7];
            String city=values[row][8];
            String state=values[row][9];
            String Zip=values[row][10];
            String country=values[row][11];
            String phone=values[row][12];
            String Findus=values[row][13];
            String Mobile=values[row][14];
            driver.findElement(By.name("firstname")).sendKeys(First);
            driver.findElement(By.name("lastname")).sendKeys(Last);
            driver.findElement(By.name("companyname")).sendKeys(Company);
            driver.findElement(By.name("email")).sendKeys(Email);
            driver.findElement(By.id("inputNewPassword1")).sendKeys(Password);
            js=(JavascriptExecutor)driver;
            js.executeScript("scroll(0,300)");
            driver.findElement(By.id("inputNewPassword2")).sendKeys(CnfPassword);
            driver.findElement(By.name("address1")).sendKeys(Add1);
            driver.findElement(By.name("address2")).sendKeys(Add2);
            driver.findElement(By.name("city")).sendKeys(city);
            driver.findElement(By.name("state")).sendKeys(state);
            WebElement pin=driver.findElement(By.name("postcode"));
            js.executeScript("arguments[0].scrollIntoView(true);",pin);
            pin.sendKeys(Zip);
            WebElement Country=driver.findElement(By.xpath("//select[@id='country']"));
            js.executeScript("arguments[0].scrollIntoView(true);",Country);
             Thread.sleep(2000);
            Select count=new Select(Country);
            count.selectByVisibleText(country);
             Thread.sleep(2000);
            driver.findElement(By.name("phonenumber")).sendKeys(phone);
             Thread.sleep(2000);
            WebElement find=driver.findElement(By.id("customfield1"));
             Thread.sleep(2000);
            Select findus=new Select(find);
            findus.selectByVisibleText(Findus);
             Thread.sleep(2000);
            driver.findElement(By.xpath("//input[@id='customfield2']")).sendKeys(Mobile);
            Thread.sleep(10000);
           WebElement submit=driver.findElement(By.xpath("//input[@class='btn btn-large btn-primary']"));
           if(submit.isDisplayed()){
               act.moveToElement(submit);
               submit.click();
           }else
           {
               act.moveToElement(submit);
               wait=new WebDriverWait(driver, 10);
               wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='btn btn-large btn-primary']")));
               submit.click();
           }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您每列创建一个新数组,基本上可以删除之前存储的内容。

for(/*alltherows*/){

    for(/*all the columns */){
        //THIS IS YOUR ERROR!! You're creating a new array, and storing it in values
        values=new String[data.sheet.getLastRowNum()+1][data.sheet.getRow(row).getLastCellNum()+1];
        //Now you're putting a value in one spot on the array
        values[row][col]=data.sheet.getRow(row).getCell(col).getStringCellValue();

    }
    //data access stuff
}

解决方案是在for循环之外创建数组。如果您的所有行都具有相同数量的列,则可以对您要查看的行进行硬编码:

//create a new array before you use it
values=new String[data.sheet.getLastRowNum()+1][data.sheet.getRow(1).getLastCellNum()+1];
for(/*alltherows*/){

    for(/*all the columns */){
        //Now you're putting a value in one spot on the array
        values[row][col]=data.sheet.getRow(row).getCell(col).getStringCellValue();

    }
    //data access stuff... NO LONGER NULL :)!!
}

一些进一步的想法......如果你只是每行访问数据,那么多维数组就会变得不必要了。试着弄清楚是否可以将其更改为一维数据结构以满足您的需求。它将为您和其他需要解释代码的人带来麻烦。