2015-07-21 108 views
0

我在我的while循環上得到一個java.lang.NullPointerException我的afterMigration方法它不能正常工作。我也收到所有if語句preparedStatements.setString(..) and setInt(..)的所有警告。請我會很感激任何迴應。我的結果集while循環中的JDBC java空指針異常

在這個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

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語句中的所有代碼將永遠不會被執行,因此是警告。

編輯:

正如馬克Rotteveel指出,cosprofiletype == null拋出NullPointerException

我還建議對使用==比較字符串閱讀本:

Java String.equals versus ==

+0

'cosprofiletype == 「」'不會引發'NullPointerException' –