你好,谁能告诉我为什么我在这个代码中得到了空指针异常:cell = sheet.getRow(init_cell).getCell(0); 初始化单元=7;但是当我用 7 替换上一行中的变量时,空指针消失了。
查询的结果集有多行。
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();
}
}