Java:如何在另一个类中传递和使用此数组

时间:2013-08-09 08:34:59

标签: java testng

我有一个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(数组);显示数组列表。

编辑:我发现了问题所在: 首先,我改变了将string []初始化为此

的方式
String[] firstname = new String[2];
String[] lastname = new String[2];

现在firstname [0]返回一个值。

但是,当我在实际包含测试用例的下一个方法中尝试system.out.println firstname [0]时,它返回null。

所以我必须找到一种方法将这些字符串传递给该方法。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我通过大量重构代码找到了解决这个问题的方法。 这一切都在这里解释

Java: how can I put two String[] objects into one object and pass them to the next method?