JDBC java空指针异常在我的结果集while循环



我在我的afterMigration方法上的while循环上得到了一个java.lang.NullPointerException,它不能正常工作。我也得到了所有if语句preparedStatements.setString(..) and setInt(..)的警告。如有任何回复,我将不胜感激。

在这个jdbc代码中,我连接到我的本地数据库。我首先选择一个方法中的记录,而不是在下一个afterMigration方法上更新。

public static void main (String[] args) throws Exception  {
    ResultSet rs = null;
    Connection bidsConnection = null;
    MyDatabaseTest migration = new MyDatabaseTest();
    try {
        bidsConnection = migration.getOracleConnection();
        rs = migration.selectRecordsBids(bidsConnection);
        migration.afterMigration(bidsConnection, rs);

    } catch (Exception e){
        e.printStackTrace();
    }
    finally {
        if (migration.scanner != null) {
            migration.scanner.close();
            System.out.println("Close Scanner");
        }
        if (rs != null) {
            rs.close();
            System.out.println("Close ResultSet");
        }
        if (migration.preparedStatement != null) {
            migration.preparedStatement.close();
            System.out.println("PreparedStatement is closed");
        }
        if (bidsConnection != null) {
            bidsConnection.close();
            System.out.println("Bids Connection is closed");
        }
    }
}

public ResultSet selectRecordsBids(Connection dbConnection) throws SQLException, ClassNotFoundException {

    Statement statement = null;
    String selectTableSQL = "SELECT traffic_profile_id from TRAFFIC_PROFILE_temp"
            + "   where traffic_profile_id >= ? AND traffic_profile_id <= ? ";

    System.out.println("Bids Select Statement: " +  selectTableSQL);
    preparedStatement = dbConnection.prepareStatement(selectTableSQL);
    preparedStatement.setInt(1, 1);
    preparedStatement.setInt(2, 1000);
    // execute select SQL statement
    ResultSet res = preparedStatement.executeQuery();
    //Gets the max profile_id record
    statement = dbConnection.createStatement();
    ResultSet r = statement.executeQuery("SELECT traffic_profile_id AS rowcount FROM TRAFFIC_PROFILE_temp WHERE pe_egress_flag IS NULL "
             +"  AND pe_ingress_flag IS NULL "
             +"  AND ce_ingress_flag IS NULL "
             +"  AND ce_egress_flag IS NULL "
             +"  AND cos_profile_type IS NULL "
             +"  AND traffic_profile_id >= 1 AND traffic_profile_id <= 1000 ");       
    r.next();
    int nullValues = r.getInt("rowcount");
    System.out.println("Query for null valies " + nullValues);
    r.close();
    statement.close();
    System.out.println("TRAFFIC_PROFILE_temp table has null traffic_profile_id of " + nullValues );
    return res;

}

public void afterMigration(Connection dbConnection, ResultSet rs) throws Exception {

    PreparedStatement preparedStatement = null;
    String update1 = "update traffic_profile_temp set cos_profile_type = ?, pe_ingress_flag = ?, pe_egress_flag = ?, ce_ingress_flag = ?, ce_egress_flag = ?"
            + "  where traffic_profile_id >= ? AND traffic_profile_id <= ?";
    String update2 = "update traffic_profile_temp set cos_profile_type = ?, pe_ingress_flag = ?, pe_egress_flag = ?, ce_ingress_flag = ?, ce_egress_flag = ? "
            +  " where cosModel = ? ";
            System.out.println("BIDS Manual Updating After Migration Statement: " + update1);
            System.out.println("Updating Bids Database in process ...");
            preparedStatement = dbConnection.prepareStatement(update1);

            while (rs.next()) { 
                String cosprofiletype = rs.getString("cos_profile_type");
                String peingressflag = rs.getString("pe_ingress_flag");
                String peegressflag = rs.getString("pe_egress_flag");      
                String ceingressflag = rs.getString("ce_ingress_flag"); 
                String ceegressflag = rs.getString("ce_egress_flag");  
                int trafficprofileid = rs.getInt("traffic_profile_id");  
                String cosModel = rs.getString("cosModel");     

            if ((trafficprofileid > 0 && trafficprofileid < 25 ) && cosprofiletype == null && cosprofiletype == "" &&
              peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null) {
            System.out.println("First if statement");
            preparedStatement.setString(1, "LEGACY");               
            preparedStatement.setString(2, "T");
            preparedStatement.setString(3, "T");    
            preparedStatement.setString(4, "F");
            preparedStatement.setString(5, "F");
            preparedStatement.setInt   (6, 1);
            preparedStatement.setInt   (7, 24);
            preparedStatement.addBatch();
            }  
            else if ((trafficprofileid > 126 && trafficprofileid < 138 ) && cosprofiletype == null && cosprofiletype == ""  &&
                      peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null)  {
                System.out.println("2 if statement");
            preparedStatement.setString(1, "STANDARD");
            preparedStatement.setString(2, "T");
            preparedStatement.setString(3, "T");    
            preparedStatement.setString(4, "F");
            preparedStatement.setString(5, "F");
            preparedStatement.setInt   (6, 127);
            preparedStatement.setInt   (7, 137);
            preparedStatement.addBatch();
            }
            else if ((trafficprofileid > 799 && trafficprofileid < 900 ) && cosprofiletype == null && cosprofiletype == ""  &&
                      peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null ) {
                System.out.println("3 if statement");
            preparedStatement.setString(1, "STANDARD");
            preparedStatement.setString(2, "T");
            preparedStatement.setString(3, "T");    
            preparedStatement.setString(4, "F");
            preparedStatement.setString(5, "F");
            preparedStatement.setInt   (6, 800);
            preparedStatement.setInt   (7, 899);
            preparedStatement.addBatch();
            }
            System.out.println("BIDS Manual Update2 Statement: " + update2);
            System.out.println("Updating Bids Database in process ...");
            preparedStatement = dbConnection.prepareStatement(update2,  ResultSet.TYPE_SCROLL_INSENSITIVE,
                     ResultSet.CONCUR_UPDATABLE);
            if  ((cosModel == "optB" ) && cosprofiletype == null && cosprofiletype == ""  &&
                      peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null )  {
                System.out.println("4 if statement");
            preparedStatement.setString(1, "optionB");
            preparedStatement.setString(2, "T");
            preparedStatement.setString(3, "T");    
            preparedStatement.setString(4, "F");
            preparedStatement.setString(5, "F");
            preparedStatement.setString(6, "optB");
            preparedStatement.addBatch();
            }
    }
            int[] affectedRecords =preparedStatement.executeBatch();
            dbConnection.commit();
}

您得到的java.lang.NullPointerException和警告是由于您的if语句的逻辑。

例如,取第一个if语句:

if ((trafficprofileid > 0 && trafficprofileid < 25)
        && cosprofiletype == null && cosprofiletype == ""
        && peingressflag == null && peegressflag == null
        && ceingressflag == null && ceegressflag == null) {
    //Dead code warning here
}

cosprofiletype为空时,条件cosprofiletype == null为真,因此代码继续并到达cosprofiletype == "",触发NullPointerException。

这个逻辑也使得if语句永远不会为真(一个为null的对象永远不会为空)。这意味着if语句中的所有代码永远不会被执行,因此出现了警告。

编辑:

正如Mark Rotteveel所指出的,cosprofiletype == null抛出NullPointerException

我还建议阅读这篇关于使用==比较字符串的文章:

Java字符串。== =

最新更新