views:

433

answers:

5

Hi I'm getting nullpointerexception at rs.next() or rs.getString(1) it is really weird that sometimes rs.next() works fine and it throws nullpointerexception at rs.getString("PRODUCTCODE"),sometimes it throws npe at rs.getString("PRODDATE") i dont understand why rs.getString() thows npe while rs.next() works fine

Here is my code

{ 
ResultSet rs = null;
String query = "";
BarcodeBean bi = null;
try {
query = "SELECT * FROM TABLE(GET_BARCODEINFO('"barcode.trim()"'))";

statement = connection.createStatement();
Logger.getInstance().getLogger().logInfo(query);
rs = statement.executeQuery(query);

bi = new BarcodeBean();

if (rs == null){
if(rs.next()){

bi.setUrunKodu(rs.getString("PRODUCTCODE"));
bi.setImalatMakineKodu(rs.getString("PRODMACHINECODE")); 
bi.setOperatorSicilNo(rs.getString("OPERATORID")); 
bi.setImalatTarihi(rs.getString("PRODDATE")); 
bi.setImalatVardiyasi(rs.getString("PRODSHIFT"));
bi.setSeriNumarasi(rs.getString("SERIALNUMBER"));
bi.setSirtTarihi(rs.getString("SIRTTARIHI"));
}
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} catch (Exception e) {
e.printStackTrace();
} finally {

DatabaseUtility.close(rs);
DatabaseUtility.close(statement);

}
} 
+2  A: 

probably you want:

if (rs != null) {
   if (rs.next()) {

or even better:

if (rs != null && rs.next()) {

Your test is simply wrong.

dfa
Roman
Your test is wrong as well. In harigm's code, rs can't possibly be null, when rs.next() is invoked.
jarnbjo
James P.
A: 

You made a wrong notation in if condition,

So change it as, if(rs!=null){ ...... .......}

Venkats
+1  A: 

From Statement javadocs:

public ResultSet executeQuery(String sql) throws SQLException

Returns: a ResultSet object that contains the data produced by the given query; never null

So, you don't need to verify whether rs == null.

The standard way to proceed resultset rows is:

while (rs.next ()) {
   doSmth (rs);
} 
Roman
A: 

hi dfa is right. i wrote is wrong

it must be if (rs != null) { if (rs.next()) {

cemil_j
+1  A: 

Statement#executeQuery() never returns null. The whole nullcheck is superfluous. The normal idiom is the following:

public Entity find(Long id) throws SQLException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    Entity entity = null;

    try {
        connection = database.getConnection();
        preparedStatement = connection.prepareStatement(SQL_FIND);
        preparedStatement.setLong(1, id);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            entity = new Entity();
            entity.setId(resultSet.getLong("id"));
            entity.setName(resultSet.getString("name"));
            // ...
        }
    } finally {
        close(connection, preparedStatement, resultSet); // In reversed order.
    }

    return entity;
}

Your actual problem lies somewhere else. This is clearly a misinterpretation of the stacktrace. To nail it correctly down, you need to lookup the line number of the first line in the stacktrace and point us the exact code at this line number.

BalusC