简体   繁体   English

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

[英]my array storing the values only until iterations runs..after completing the storing of values and trying to get those values it returns Null values

my array storing the values only until iterations runs..after completing the storing of values and trying to get those values it returns Null values and tried so many times and so many ways to get the values but there is no use in my attempts...finally i ended up with stack overflow website.i hope my wait ends here 我的数组仅在迭代运行之前存储值。.在完成值的存储并尝试获取这些值之后,它返回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();

            }

//after the column iteration completed i tried to get the values from array and stored in string by below code, but it returns null values.. i dont know where i am getting error but my array returns null values for the below indexs even it filled with data //在完成列迭代之后,我尝试从数组中获取值并通过以下代码存储在字符串中,但它返回空值。.我不知道我在哪里出错,但是我的数组为以下索引返回了空值,即使它充满数据

          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();
           }
        }
    }
}

You're creating a new array per column, essentially erasing what was stored before it. 您正在为每列创建一个新的数组,实质上是擦除之前存储的内容。

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
}

The solution is to create your array outside of your for loops. 解决方案是在for循环之外创建数组。 If all of your rows have the same amount of columns you can hard code the row you look at for this: 如果您所有的行都具有相同的列数,则可以对此进行硬编码:

//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 :)!!
}

Some further ideas... If you're only accessing the data per row, a multidimensional array is needlessly complicated. 一些进一步的想法...如果您仅按行访问数据,那么多维数组就不必要了。 Try to figure out if you can change this to a one dimensional data structure to meet your needs. 尝试弄清楚是否可以将其更改为一维数据结构以满足您的需求。 It will save you headaches down the road as well as for anybody else who needs to interpret the code. 它将为您和其他需要解释代码的人免去头疼的麻烦。

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

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