sonofhawk01
sonofhawk01

Reputation: 11

Getting an"index:0" and "size:0" error from ArrayList.size()

I have been working on this program for hours, and have worked out nearly all of the errors that have cropped up in every other class. This one, however, I can's seem to fix the receive method. The receive method should first check (by comparing SKUs) whether or not the incoming product is already in myCatalog. If not, add the product (via its SKU) to the catalog. After that, in any case -- as long as there is still some of the incoming product not yet been placed in a bin -- I want to locate which bin contains the least total quantity. (If all the bins are full, add a new, empty bin to use)I'd add that specific product to that bin until the bin is either full -- at which point I'd repeat -- or I run out of the product. This repeats until no more incoming product is left.

public class Warehouse
{
    private int myBinMax;
    private ArrayList<MusicMedia> myCatalog;
    private ArrayList<Bin> myBins;
    public Warehouse( int binMax )
    {
        myBinMax = binMax;
        myCatalog = new ArrayList<MusicMedia>();
        myBins = new ArrayList<Bin>( 5 );
    }

    public String toString()
    {
        return Tester.detailedInventory(myCatalog,myBins);
    }

    public void addBin()
    {
        myBins.add( new Bin( "B" + myBins.size() ) );
    }

    public void receive( MusicMedia product, int quantity )
    {
        boolean contained = false;
        int lowestAmount = myBinMax,
        leastFullBinIndex,
        i,
        insertQuantity;

        for(MusicMedia entry : myCatalog)
        {
            if(entry.getSKU().equals(product.getSKU()))
            {
                contained = true;
            }
        }

        if(!contained)
        {
            myCatalog.add(product);
        }

        while(quantity > 0)
        {
            lowestAmount = myBinMax;
            for(i = 0; i < myBins.size(); i++)
            {
                if(myBins.get(i).totalQuantity() < lowestAmount)
                {
                    lowestAmount = myBins.get(i).totalQuantity();
                    leastFullBinIndex = i;
                }

                if((i == myBins.size() - 1 && lowestAmount == myBinMax)||(myBins.size() == 0))
                {
                    myBins.add(new Bin("bin" + Integer.toString(i)));
                }
            }

            if(quantity >= myBinMax - lowestAmount)
            {
                insertQuantity = myBinMax - lowestAmount;
            }
            else
            {
                insertQuantity = quantity;
            }
            quantity -= insertQuantity;
            myBins.get(i).add(new BinItem(product.getSKU(), insertQuantity));
        }
    }
}

Before my last edit, nothing would print -- just a blank console -- but in an attempt to fix that, I added this (also contained within the code above):

if((i == myBins.size() - 1 && lowestAmount == myBinMax)||(myBins.size() == 0))
{
myBins.add(new Bin("bin" + Integer.toString(i)));
}

Now, it won't run without giving me an error:

java.lang.IndexOutOfBoundsException
Index: 0, Size 0 (in java.util.ArrayList)

As well as highlighting the following line:

myBins.get(i).add(new BinItem(product.getSKU(), insertQuantity));

Here's a breakdown on the various classes that have been created for this colossal program:

MusicMedia has three private variables, but the only one that has any bearing here is the String mySKU, obtained by getSKU()

Bin has a private String, name, which is also its only argument, as well as a private ArrayList of class BinItem. In order to access this ArrayList of BinItems from another class, you would use getContents(). It also has method totalQuantity(), which returns the total quantity of objects already in a bin.

BinItem has private variables mySKU (String) and myQuantity (int) and methods getSKU() and getQuantity(), both being its arguments in that order

Upvotes: 1

Views: 2716

Answers (1)

NameSpace
NameSpace

Reputation: 10177

So to start with you have no bins.

Your "for" block is skipped because size is zero;

So we get to the final line of the "while" loop, and we execute myBins.get(i)... This is where we get the error because we can't get the bin at index 0, because we got no bins at all.

The problem is we reached this line of code without adding any bins...

I'm not sure if you realized, the line below doesn't start your program with 5 bins.

myBins = new ArrayList<Bin>( 5 );

The 5 in this code sets the internal buffer for the array to hold 5 items. If we supplied no number it would start with 10. But the numbers are just there to give programmers control over how much memmory is initalized.

If your goal was to start with 5 bins, you should do something as follows:

myBins = new ArrayList<Bin>( 5 );

for(int i=0; i<5; i++){
    myBins.add(new myBin());
}

Upvotes: 1

Related Questions