2017-02-21 1 views
-1

Excel 워크 시트의 내용을 읽으려고하지만 내 메서드가 예외를 계속 throw합니다. 나는 소용이없는 모든 것을 시도했지만, 나는 그것이 내가 볼 수없는 아주 사소한 세부 사항이라고 확신한다. 누군가가 내 코드를 신선한 눈으로 보면서 아마도 내가 잘못하고있는 것을 지적 할 수있을 것입니다. 감사!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

예외를 throw하는 행은 무엇입니까? – Lemonov

+0

getCellData() 메소드, 4 행 –

+2

'worksheet'은 null이어야합니다 - 'sheetName'이 유효합니까? –

답변

-2

발견. 나는 클래스 파일들과 함께 패키지 안에 excel 파일을 저장했고 그것을 발견 할 수 없었다. 나는 왜 그런지 모른다. 그래서 프로젝트의 루트에있는 폴더로 옮겼습니다.

관련 문제