2012-07-06 2 views
-1

널 포인터 예외가 발생하지만 이유는 알 수 없습니다. 문자열을 읽기 전에 셀이 null인지 확인했습니다. 그래서, 왜 그 문자열은 null입니까?Null 포인터 예외

private void fillArray() 
{ 
    try 
    { 
     readBook = new HSSFWorkbook(readFile); 
    } 
    catch (IOException e) 
    { 
     System.out.println("If we know what we're doing, no one should ever see this line."); 
    } 
    if (readBook != null) 
    {HSSFSheet infoSheet = readBook.getSheetAt(0); 
     HSSFRow headingsRow = infoSheet.getRow(0); 
     int i = 0; 
     HSSFCell cell = headingsRow.getCell(i); 
     String columnHeading = cell.toString(); 
     while (cell != null && !(cell.toString().equals(""))) 
     { 
      cell = headingsRow.getCell(i); 
      columnHeading = cell.toString(); 
      columnHeadings.add(columnHeading); 
      i++; 
     } 
     if(columnListIsSetup == false) 
     { 
      createList(); 
      columnListIsSetup = true; 
     } 
    } 
+7

NullPointerException이 던지는 * 수 * 많은 장소가있다 - 무엇을 스택 트레이스가 말하는가? –

+0

예외 추적은 columnHeading = cell.toString(); – user1507835

+2

그러면'cell'이 null인지 아니면'toString' 내에 던져 졌는지 보여줍니다. –

답변

0

확인 셀을 만드는 것은 전에 널 (또는) 빈, 당신은 세포가 null의 경우 경우에 NullPointerException이 원인이 될 수 String columnHeading = cell.toString();을 다하고 있습니다. 나는 당신이 그것을 변경하고자하는 의심

while (cell != null && !(cell.toString().equals(""))) 
{ 
    // We know that cell isn't null before this line... 
    cell = headingsRow.getCell(i); 

    // ... but now we've got a new value for cell, which could be null 
    columnHeading = cell.toString(); 
    columnHeadings.add(columnHeading); 
    i++; 
} 

:

+0

고마워, 나는 그것을 시도하고 여전히 예외를 받고있다. 그것은 너무 이상했기 때문에 나는 그것을 무작위로하고 있었고, 지금은 그것을 실행할 때마다 예외가 생겨서 아무 것도 바뀌지 않았습니다. – user1507835

+0

... 무작위로했기 때문에 ... – user1507835

+3

stacktrace로 질문을 업데이트하십시오. – kosa

5

나는이 문제라고 생각

while (cell != null && !(cell.toString().equals(""))) 
{ 
    // We know cell isn't null for this... 
    columnHeading = cell.toString(); 
    columnHeadings.add(columnHeading); 

    i++; 
    // It's fine to set cell to null here... we'll be 
    // checking again in a second... 
    cell = headingsRow.getCell(i); 
} 
1
while (cell != null && !(cell.toString().equals(""))) { 
    cell = headingsRow.getCell(i);  // here, cell gets reassigned so the 
             // "cell != null" check in the while 
             // loop condition loses it's value, 
             // you need to check again 

    if (cell == null)  // add the following to make sure the NEW cell value is not null 
     break;    // 

    columnHeading = cell.toString(); 
    columnHeadings.add(columnHeading); 
    i++; 
}