簡體   English   中英

Java:如何在另一個類中傳遞和使用此數組

[英]Java: how to pass and use this array in another class

我有一個CSV閱讀器類,還有一個用戶創建者類。 我希望用戶創建者類采用由CSV閱讀器生成的數組,並將數據分配給某些變量,但是我得到了nullpointerexception

這是CSV閱讀器類:

public class CSVData {

    private static final String FILE_PATH="C:\\250.csv";

    @Test
    public static void main() throws Exception {

        CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
        ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            ArrayList<String> list = new ArrayList<String>();
            for (int i=0;i<5;i++) { //5 is the number of sheets
               list.add(nextLine[i]);
            }
            array.add(list);

        }

        /*for(int x=0;x<array.size();x++) {
            for(int y=0;y<array.get(x).size();y++) {          
            }
        }*/
        AppTest3 instance = new AppTest3();
        instance.settingVariables(array);
        reader.close();
        }
    }

這是用戶創建者類

public class AppTest3 extends AppData (which extends CSVData) {

    private String[] firstname;
    private String[] lastname;

    public void settingVariables(ArrayList<ArrayList<String>> array) {
        int randomUser1 = randomizer (1, 250);
        int randomUser2 = randomizer (1, 250);
        String firstname1 = array.get(randomUser1).get(0);
        String firstname2 = array.get(randomUser2).get(0);
        String lastname1 = array.get(randomUser1).get(1);
        String lastname2 = array.get(randomUser2).get(1) ;
        //firstname = { firstname1, firstname2 }; //this doesnt work, dunno why
        //lastname = { lastname1, lastname2 };
        firstname[0] = firstname1.replace(" ", "");
        firstname[1] = firstname2.replace(" ", "");
        lastname[0] = lastname1.replace(" ", "");
        lastname[1] = lastname2.replace(" ", "");
    }
@Parameters({ "driver", "wait" })
@Test(dataProvider = "dataProvider")
public void oneUserTwoUser(WebDriver driver, WebDriverWait wait)
        throws Exception {
        // A user signs up, then signs out, then a second user signs up
        for (int y = 0; y < 2; y++) {
            String email = firstname[y].toLowerCase() + randomNumber + "@"
                    + lastname[y].toLowerCase() + emailSuffix;
    //test here
    }
}

這是錯誤消息

FAILED: oneUserTwoUser(org.openqa.selenium.support.events.EventFiringWebDriver@5efed246, org.openqa.selenium.support.ui.WebDriverWait@2b9f2263)
java.lang.NullPointerException
    at com.pragmaticqa.tests.AppTest3.oneUserTwoUser(AppTest3.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

PS:System.out.println(firstname [0]); 在控制台中不顯示任何內容。 System.out.println(array); 顯示數組列表。

編輯:我發現了問題所在:首先,我更改了將string []初始化的方式

String[] firstname = new String[2];
String[] lastname = new String[2];

現在,firstname [0]返回一個值。

但是,當我嘗試在實際包含測試用例的下一個方法中使用system.out.println firstname [0]時,它將返回null。

因此,我必須找到一種將這些字符串傳遞給該方法的方法。

在您的main方法instance中,只是一個局部變量,該局部變量在方法執行結束后會丟失:

 AppTest3 instance = new AppTest3();  // just local variable that is lost after method execution ends.
 instance.settingVariables(array);

因此,在沒有設置參數的另一個實例上調用oneUserTwoUser 您可以使用調試器看到這一點。

你可以把初始化方法before ,如下為Apptest3類:

public class AppTest3 {

  AppTest3 instance; // field used by tests

  @BeforeSuite(alwaysRun = true) 
  public void before() throws Exception {

    CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
    ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i=0;i<5;i++) { //5 is the number of sheets
           list.add(nextLine[i]);
        }
        array.add(list);

    }

    instance = new AppTest3();  /// init your instance here
    instance.settingVariables(array);
    reader.close();
    }
  }

我已經通過大量重構代碼找到了解決該問題的方法。 這一切都在這里解釋

Java:如何將兩個String []對象放入一個對象並將它們傳遞給下一個方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM