2013-09-27 5 views
0

안녕하세요 누구든지 충분히 친절하고 왜이 코드에서 널 포인터 예외가 발생하는지 정확히 알 수 있습니다. cell = sheet.getRow (init_cell) .getCell (0); init_cell = 7; 하지만 위의 줄에있는 변수를 7로 바꾸면 nullpointer가 사라집니다.Apache POI : 왜 Null 포인터 예외가 발생합니까?

쿼리의 결과 집합에는 행이 여러 개 있습니다.

public static void main(String[] args) throws Exception 
{ 
    int init_cell=7; 
    try { 
     Connection con = null; 
     Statement st = null; 
     ResultSet rs = null; 
     con = DriverManager.getConnection(
       "jdbc:postgresql://127.0.0.1:5432/DB", "xxxx", 
       "xxxx"); 

     st = con.createStatement(); 
     rs = st.executeQuery("select * from vs;"); 
     FileInputStream input_template = new FileInputStream(new File("C:\\Users\\xxxx\\Desktop\\ExcelWriteTest\\template-audit.xls")); 
     HSSFWorkbook template = new HSSFWorkbook(input_template); 
     HSSFSheet sheet = template.getSheet("Synthesis"); 

     while(rs.next()) 
     { 
      Cell cell = null; 
      cell = sheet.getRow(init_cell).getCell(0); 
     cell.setCellValue(rs.getString("serial_number")); 
     ++init_cell; 

     } 

     input_template.close(); 
     FileOutputStream out_template =new FileOutputStream(new File("C:\\Users\\xxxx\\Desktop\\ExcelWriteTest\\newaudit.xls")); 
     template.write(out_template); 
     out_template.close(); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


} 

답변

1

수단 정수 인수 걸릴 것 getRow() 함수 "특정 행 여기 7 행 예 말할를, 첫번째 셀 [즉 (0) getCell이다]"는 것 값을 삽입하십시오.

관련 문제