-1

I am getting a java.lang.NullPointerException on my while loop on my afterMigration method it is not working properly. And I am also getting all warnings on all my if statement preparedStatements.setString(..) and setInt(..). Please I would appreciate any responses.

In this jdbc code I am connecting to my local db. I am first selecting the records in one method than I am updating on the next afterMigration method.

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();
}
yesco1
  • 371
  • 2
  • 7
  • 22

1 Answers1

1

The java.lang.NullPointerException and the warnings you are getting are due to the logic of your if-statements.

For instance, take the first if-statement:

if ((trafficprofileid > 0 && trafficprofileid < 25)
        && cosprofiletype == null && cosprofiletype == ""
        && peingressflag == null && peegressflag == null
        && ceingressflag == null && ceegressflag == null) {

    //Dead code warning here
}

When cosprofiletype is null, the condition cosprofiletype == null is true, so the code continues and hits cosprofiletype == "", which triggers the NullPointerException.

This logic also makes it so that the if-statement is never true (an object that is null is never empty). This means all code inside the if-statements will never be executed, hence the warnings.

EDIT:

As pointed out by Mark Rotteveel, cosprofiletype == null will not throw a NullPointerException.

I would also recommend reading this on the use of == to compare strings:

Java String.equals versus ==

Community
  • 1
  • 1