循环时我的结果集上的JDBC java null指针异常

时间:2015-07-21 20:22:04

标签: java jdbc

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

1 个答案:

答案 0 :(得分:1)

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 String.equals versus ==