读取excel文件因空锁指针异常而失败

时间:2013-05-23 21:25:41

标签: java nullpointerexception

我是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)

先谢谢     干杯

1 个答案:

答案 0 :(得分: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,所以我无法测试。