1

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();

4
  • Yes, I know that my sql statement is ridiculous :) I will exchange it on a procedure later. Commented Jul 11, 2020 at 20:32
  • 1
    What is the question here? Are you looking for a better way to construct the query and set the parameters? Commented Jul 11, 2020 at 20:49
  • (a) Why do you have one flat table in the database yet a hierarchy of objects in Java? (b) What exactly is your question? (c) Why the tag for servlets? (d) java.sql.Timestamp was supplanted years ago by the java.time.Instant class as of JSR 310 and JDBC 4.2. Commented Jul 11, 2020 at 22:08
  • The question is: How to retrieve every single every single class attribute of a class included in an object named "material", which is included in an arraylist? Commented Jul 12, 2020 at 12:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You should wrap any code in backticks `` so that it's formatted as code. It helps distinguish between normal text and code when quickly reading through and it clarifies your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.