1

I have two classes:

Products:

01; Desinfectante
02; Aerosol
03; Limpia Vidrio
04; Desengrasante
05; Mata mosquitos
06; Mata cucarachas
07; Aceite en aerosol

Instructions:

01;1;Elevar la masa hasta llegar a tal punto;0;10
01;1;Mezclar este material con anterior;1;15
01;2;Relevar;2;5
01;3;Llevar;00;0
02;1;Descripcion;7;2
02;2;Descripcion;6;2
02;2;Descripcion;00;0
03;1;Descripcion;1;1
03;1;Descripcion;2;9
03;2;Descripcion;00;0
03;3;Descripcion;5;2
03;4;Descripcion;6;2
03;4;Descripcion;3;10
04;1;Descripcion;00;0
04;2;Descripcion;1;2
04;3;Descripcion;1;0
04;3;Descripcion;2;2
04;3;Descripcion;3;2
04;4;Descripcion;7;1
04;4;Descripcion;6;2
05;1;Descripcion;7;20
05;1;Descripcion;6;9
05;2;Descripcion;00;0
05;3;Descripcion;1;2
05;3;Descripcion;2;10
06;1;Descripcion;2;12
06;1;Descripcion;4;1
06;1;Descripcion;6;8
06;2;Descripcion;5;4
06;2;Descripcion;7;2
07;1;Descripcion;1;12
07;1;Descripcion;2;2
07;2;Descripcion;3;19
07;2;Descripcion;4;4
07;2;Descripcion;00;2
07;2;Descripcion;5;12

The thing is this: i have to insert the instructions ArrayList into the Products. The link between them is the first number, that is the code of the product.

I tried two things, the first one:

public static ArrayList<Productos> InsertInstInProd(ArrayList<Instrucciones> instructions, ArrayList<Productos> products)
{ 
    for (int i = 0; i < products.size()-1; i++)
    {
        int n = 0;
        for (int j = 0; j < instructions.size()-1; j++)
        {
            int first = products.get(i).getNumero();
            int second = instructions.get(j).getCodProd();

            if (first == second)
            {
                products.get(i).getInstr().get(n).setCodIns(instructions.get(j).getCodIns());
                products.get(i).getInstr().get(n).setCodProd(instructions.get(j).getCodProd());
                products.get(i).getInstr().get(n).setDescr(instructions.get(j).getDescr());
                products.get(i).getInstr().get(n).setMat(instructions.get(j).getMat());
                products.get(i).getInstr().get(n).setMatNec(instructions.get(j).getMatNec());

                n++;
            }
        }
        n = 0;
    }

The second one:

public static ArrayList<Productos> InsertInstInProd(ArrayList<Instrucciones> instructions, ArrayList<Productos> products)
{ 
    for (int i = 0; i < products.size()-1; i++)
    {
        int n = 0;
        for (int j = 0; j < instructions.size()-1; j++)
        {
            int first = products.get(i).getNumero();
            int second = instructions.get(j).getCodProd();

            if (first == second)
            {
                products.get(i).setInstr(instructions);
                n++;
            }
        }
        n = 0;
    }

    return products;
}
3
  • SO? what's your question? Commented Jul 11, 2015 at 3:10
  • In the first case i've got this: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 In the second one it doesn't work, it doesn't validate the codes of the product, so it's useless. I don't know what I'm doing wrong. Commented Jul 11, 2015 at 3:14
  • Can you post full error in the question please? Commented Jul 11, 2015 at 3:18

2 Answers 2

1

You are getting NullPointerException because of

     products.get(i).getInstr().get(n).setCodIns(instructions.get(j).getCodIns());

You are not checking whether the list products.get(i).getInstr() has elements or not. When the list is empty and when you are accessing it as products.get(i).getInstr().get(0) it's throwing you NullPointerException because trying to get the first element of an empty list. So before you do this operation, make sure that products.get(i).getInstr() is not empty.

If they are of same type, you can directly add the whole arraylist :

   products.get(i).getInstr().addAll(instructions); // again make sure that is not empty.

If you just want to replac, use :

    products.get(i).setInstr(instructions.get(j)); 
Sign up to request clarification or add additional context in comments.

2 Comments

ok, and how can I add the entire ArrayList (instructions) to products.get(i).getInstr() ?
yeah, i've tried that, but the thing is that all the ArrayList goes there, and I want only that line. products.get(i).setInstr(instructions).get(j);
0

Products Class



    package productsandinstructions;

    import java.util.List;

    public class Product {
        private int productId;
        private String productName;
        private List instructions;

        public int getProductId() {
            return productId;
        }

        public void setProductId(int productId) {
            this.productId = productId;
        }

        public String getProductName() {
            return productName;
        }

        public void setProductName(String productName) {
            this.productName = productName;
        }

        public List getInstructions() {
            return instructions;
        }

        public void setInstructions(List instructions) {
            this.instructions = instructions;
        }
    }


Instruction Class



    package productsandinstructions;

    public class Instruction {
        private int productId;
        private int instructionId;
        private String instDesc;
        private int mat;
        private int matNec;
        private boolean done;

        public int getProductId() {
            return productId;
        }

        public void setProductId(int productId) {
            this.productId = productId;
        }

        public int getInstructionId() {
            return instructionId;
        }

        public void setInstructionId(int instructionId) {
            this.instructionId = instructionId;
        }

        public String getInstDesc() {
            return instDesc;
        }

        public void setInstDesc(String instDesc) {
            this.instDesc = instDesc;
        }

        public int getMat() {
            return mat;
        }

        public void setMat(int mat) {
            this.mat = mat;
        }

        public int getMatNec() {
            return matNec;
        }

        public void setMatNec(int matNec) {
            this.matNec = matNec;
        }

        public boolean isDone() {
            return done;
        }

        public void setDone(boolean done) {
            this.done = done;
        }
    }


Main Class



    package productsandinstructions;

    import java.util.List;

    public class ProductsAndInstructionsMain {

        private List products;
        private List instructions;

        public List getProducts() {
            return products;
        }

        public void setProducts(List products) {
            this.products = products;
        }

        public List getInstructions() {
            return instructions;
        }

        public void setInstructions(List instructions) {
            this.instructions = instructions;
        }

        public static void main(String[] args) {
            ProductsAndInstructionsMain main = new ProductsAndInstructionsMain();
            main.mergeProductsAndInstructions();
        }

        public void mergeProductsAndInstructions() {
            for (Product product : products) {
                for (Instruction instruction : instructions) {
                    if ((!(instruction.isDone())) && (instruction.getProductId() == product.getProductId())) {
                        product.getInstructions().add(instruction);
                        instruction.setDone(true);
                    }
                }
            }
        }
    }


Comments

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.