Iterate throught arraylist of objects and return every single class attribute

I created five classes -> GeneralData, MaterialData, WeigthsAndDimensions, TechnicalData, LogisticData. All classes have a few attributes. I also created a class Material, whose constructor has five Object attributes:

package com.mmdmanager.others;  public class Material {     private GeneralData generalData;     private MaterialData materialData;     private WeigthsAndDimensions weigthsAndDimensions;     private TechnicalData technicalData;     private LogisticData logisticData;      public Material(GeneralData generalData, MaterialData materialData, WeigthsAndDimensions weigthsAndDimensions, TechnicalData technicalData, LogisticData logisticData) {         this.generalData = generalData;         this.materialData = materialData;         this.weigthsAndDimensions = weigthsAndDimensions;         this.technicalData = technicalData;         this.logisticData = logisticData;     }      @Override     public String toString() {         return generalData + "," + materialData + "," + weigthsAndDimensions + "," + technicalData + "," + logisticData;     } } 

My application create over one object of class "Material" and put all these objects into arraylist:

Material material = new Material(generalData, materialData, weigthsAndDimensions, technicalData, logisticData); materialList.add(material); 

I have to force an algorithm, which iterates through each attribute of all five classes included in an object "material". Also, if an arraylist materialList has more than one objects, the algorithm should iterate through each object. I created such a code:

public ArrayList<Material> getMaterialList(ArrayList<Material> materialList, GeneralData generalData, MaterialData materialData, WeigthsAndDimensions weigthsAndDimensions, TechnicalData technicalData, LogisticData logisticData) {      try {         connection = ConnectionProvider.getConnection();         while(!connection.isClosed()) {             preparedStatement = connection.prepareStatement("insert into materials(material_name,product_number,user_id,request_datetime,esk_number,request_type,request_sub_type,remark,batch_number, product_hierarchy, gross_Weight, net_Weight, material_Length, material_Width, material_Height, material_Volume, Capacity_Unit_Of_Measure, inverter, POWER_SUPPLY, CEMARK, REFR_APPLICATION, REFR_MODE, REFRIGERANT_TYPE, REFRIGERANT_WEIGHT, FREQUENCY, COMPRESSOR_TYPE, PACKAGING_STYLE, SALES_OEM_PRODUCT, BUY_OEM_PRODUCT, INDOOR_OUTDOOR, DG_INDICATOR_PROFILE, SALES_BRAND, BUSINESS_PILAR, MATERIAL_SOURCE, FACTORY_NAME, DESTINATION_MARKET) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");              connection.setAutoCommit(false);             iterator = materialList.iterator();              while (iterator.hasNext()) {                 for (counter = 1; counter < 37; counter++) {                     if ((counter > 0 && counter < 4) || (counter > 5 && counter < 11) || (counter == 32) || ((counter > 33 && counter < 37))) {                         preparedStatement.setString(counter,String.valueOf(iterator.next()));                     }                     else if (counter == 4) {                         preparedStatement.setTimestamp(counter, Timestamp.valueOf(String.valueOf(iterator.next())));                     }                     else if (counter == 5) {                         preparedStatement.setInt(counter,Integer.valueOf(String.valueOf(iterator.next())));                     }                     else if ((counter > 10 && counter < 17) || (counter > 24 && counter <25)) {                         preparedStatement.setDouble(counter, Double.valueOf(String.valueOf(iterator.next())));                     }                     else if ((counter > 16 && counter < 24) || (counter > 25 && counter < 32) || counter == 33) {                         preparedStatement.setDouble(counter, Byte.valueOf(String.valueOf(iterator.next())));                     }                 }                 preparedStatement.addBatch();             }              preparedStatement.executeBatch();             connection.commit();             connection.setAutoCommit(true);             connection.close();         }     }     catch (SQLException ex) {         ex.printStackTrace();         System.out.println(ex.getSQLState());     }     return materialList; } 

java.util.NoSuchElementException -> The folowing error occured, when counter equals 2 and an attribute cannot be assigned to preparedStatement.setString();

Add Comment
1 Answer(s)

Iterator’s next method throws a NoSuchElementException if the iteration has no more elements see Java documentation. You check whether the iterator hasNext in your while-loop, but in your for-loop you call next without checking whether there is a next element.

Let’s say your list only has one element. hasNext in the while loop returns true, you enter the for-loop. In the for-loop you retrieve the first element, increase your counter to 2 and try to retrieve the next element (which does not exist) and this causes the NoSuchElementException.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.