2017-02-21 5 views
-1

Excelワークシートの内容を読み込もうとしていますが、メソッドが例外をスローし続けます。私は無駄にすべてを試しましたが、私はそれが私が見ていない非常にマイナーな詳細だと確信しています。誰かが私のコードを新鮮な目で見て、私が間違っていることを指摘する可能性があります。ありがとう!Excelセルの内容を取得するとNullPointerExceptionが返されます

/** 
* This method sets the path to the excel file and the excel sheet as well as 
* initializing the stream 
*/ 
public static void setExcelFile(String path, String sheetName) { 
    try { 
     FileInputStream excelFile = new FileInputStream(path); 
     workbook = new XSSFWorkbook(excelFile); 
     worksheet = workbook.getSheet(sheetName); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 


/** 
* This method reads the cell data from the excel file 
*/ 
public static String getCellData(int rowNum, int colNum) { 
    try { 
     //This is what is causing my nullpointerexception according to eclipse 
     row = worksheet.getRow(rowNum); 
     //cell = worksheet.getRow(rowNum).getCell(colNum); 
     cell = row.getCell(colNum); 
     String cellData = cell.getStringCellValue(); 
     return cellData; 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     return "Lactoferrin"; 
    } 

} 


/** 
* This is how i call both methods 
*/ 
public static void main(String[] args) { 
    try { 
     ExcelUtility.setExcelFile(PATHTOTESTDATA, FILENAME); 
     String cellContents = ExcelUtility.getCellData(1,0); 
     System.out.println(cellContents); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
+0

例外をスローする行はありますか? – Lemonov

+0

getCellData()メソッド、行4 –

+2

'worksheet'はnullでなければなりません - ' sheetName'は有効ですか? –

答えて

-2

が見つかりました。私は、クラスファイルを持つパッケージ内にexcelファイルを保存していましたが、Eclipseがそれを見つけることができませんでした...なぜか分かりません。だから私はプロジェクトのルートにあるフォルダに移動し、それは働いた。

関連する問題