Reputation: 179
Hello can anyone be kind enough and tell why exactly am i getting the null pointer exception in this code on line: cell = sheet.getRow(init_cell).getCell(0); init_cell=7; but when i replace the variable in the above line with 7 the nullpointer goes away.
The result set of the query has mutiple rows.
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();
}
}
Upvotes: 0
Views: 603
Reputation: 262
The getRow() function will take an integer argument which means "In that particular Row say for eg here in the 7th row, first cell [ that is getCell(0) ]" it will insert the value.
Upvotes: 1