0

こんにちは、誰でも親切で、このコードでヌルポインター例外が発生する理由を教えてください: cell = sheet.getRow(init_cell).getCell(0); init_cell=7; しかし、上記の行の変数を 7 に置き換えると、nullpointer が消えます。

クエリの結果セットには複数の行があります。

  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();
    }


}
4

1 に答える 1

1

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.

于 2013-09-27T10:03:14.513 に答える