0

我是 Java 和编程的新手。我有一堆代码从 excel 中读取数据并将其用于运行一些测试用例(selenium webdriver)。下面的代码显示了从 excel 读取数据,这在串行运行我的测试用例时工作正常(一个接一个)。

public String getData(String SheetName, String DataSet, String ColumnName) throws                JXLException,IOException {         
        Workbook workbook=null;            
        workbook = Workbook.getWorkbook(new File(".\\data\\TestData.xls"));
        Sheet sheet = workbook.getSheet(SheetName); 
        int iRow,iCol=0,iNumCol,p;
        String sData="";      
        Cell sTestData=sheet.findCell(DataSet);
        iNumCol=sheet.getColumns();
        iRow=sTestData.getRow();
        for (p=0;p<iNumCol;p++)
        {               
            String sCol=sheet.getCell(p,0).getContents();
            if (sCol.matches(ColumnName))
            {
                iCol=p;
                bColumnFound=true;
            }   

          }      
        sData = sheet.getCell(iCol,iRow).getContents();    
        return sData;  
    }

@Test(testName = "list_News")
@Parameters("format")
public void list_News(String format) throws Exception {

String sgetname = "", sPassword = "", sUrl = ""; 
sgetname = cm.getData("list_News", "list_News", "idname");  >>> THIS STEP I am getting NULL POINTER EXCEPTION
sPassword = cm.getData("list_News", "list_News", "Password");
sUrl = cm.getData("list_News", "list_News", "URL");

My Question is - when i try to run multiple cases concurrently, I am getting Null Pointer exception as shown below. The point where its failing at every case is at the first line of reading excel file. I suspect it might be some lock caused during reading excel file. How Do I resolve this issue....is it multiThreading that i need to implement and how Do i do that?

java.lang.NullPointerException
at common.commonMethods.getData(commonMethods.java:241)
at api.case.list.Listnews.test_paperid_Pos_014(news_uth.java:282)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

提前致谢 干杯

4

1 回答 1

0

我认为你忘记测试你是否发现了价值。试试看:

public String getData(String SheetName, String DataSet, String ColumnName) throws JXLException, IOException {
    Workbook workbook = Workbook.getWorkbook(new File( ".\\data\\TestData.xls"));
    Sheet sheet = workbook.getSheet(SheetName);
    Cell sTestData = sheet.findCell(DataSet);
    int iNumCol = sheet.getColumns();
    int iRow = sTestData.getRow();
    boolean bColumnFound = false;
    int iCol;

    for (int p = 0; p < iNumCol; p++) {
        String sCol = sheet.getCell(p, 0).getContents();
        if (sCol.matches(ColumnName)) {
            iCol = p;
            bColumnFound = true;
        }

    }
    if (!bColumnFound) {
        return "";
    }
    return sheet.getCell(iCol, iRow).getContents();
}

我不知道您使用的是什么 API,所以我无法测试。

于 2013-05-23T22:13:34.020 回答