Connection connection = null; try { connection = DriverManager.getConnection("jdbc:derby://localhost:1527/database","root","root"); connection.setAutoCommit(false); PreparedStatement query = connection.prepareStatement("INSERT INTO table (number) VALUES (?)"); query.setInt(1, 12); int temp = query.executeUpdate(); Sytem.out.println(temp); ResultSet rs = query.getGeneratedKeys(); while (rs.next()) { System.out.println(rs.getInt(1)); } connection.commit(); } catch (SQLException ex) { if(connection != null){ try { connection.rollback(); } catch (SQLException ex1) { Logger.getLogger("meh").log(Level.SEVERE, null, ex1); } } Logger.getLogger("meh").log(Level.SEVERE, null, ex); }finally{ try { if(connection != null) connection.close(); } catch (SQLException ex) { Logger.getLogger("meh").log(Level.SEVERE, null, ex); } }
Why am I getting NullPointerException error on line with while? Data are inserted correctly I can check that but am still getting error on line with while.
Edit: Oh sure and data am trying to get should be id generated by DB.