Error modifying an object within a cycle

0

As the title says, I have a problem when trying to modify an attribute of an object at the time of being traversed; what I do is create a cycle to go through a list, within this cycle I create an object which I will fill from a SQLite database, and when I modify one of the attributes I get an error of NullPointerException, the funny thing is that the error comes out after having already done a cycle, and if I do not modify the object the objects fill up well.

selected = "0"+String.valueOf(i+1);
        Log.e("onItemSelected",selected);
        aList = sqLiteAccumulated.getAllAccumulated(selected);

        sqLiteItems = new SQLiteItems(ItemActivity.this);
        pList = new ArrayList<ProductModel>();
        Log.e("aList size", aList.size()+"");
        for (int j = 0; j<aList.size(); j++){
            productModel = new ProductModel();
            productModel = sqLiteItems.readItem(aList.get(j).getMI2_ARTIC());
            productModel.setMI_ACT(Float.parseFloat(aList.get(i).getMI2_ACT()));
            if (productModel!=null){
                pList.add(productModel);
            }
        }

and the log shows me this

java.lang.NullPointerException: Attempt to invoke virtual method 'void co.dibso.gomezvelasquezvendedores.Modelo.ProductModel.setMI_ACT(float)' on a null object reference

but this happens after having already gone through a cycle.

    
asked by mlaguirre 12.10.2017 в 03:45
source

1 answer

0

The method readItem is returning null and still you execute the method setMI_ACT without validating that it is not null.

Try to check first if this is null and then execute the method:

    //...
    for (int j = 0; j<aList.size(); j++){
        productModel = new ProductModel();
        productModel = sqLiteItems.readItem(aList.get(j).getMI2_ARTIC());
        if (productModel!=null){
            productModel
            .setMI_ACT(Float.parseFloat(aList.get(i).getMI2_ACT()));
            pList.add(productModel);
        }
    }
    
answered by 12.10.2017 / 04:43
source