Java arraylist incompatible types error

Rainier picture Rainier · Oct 21, 2013 · Viewed 12.5k times · Source

The answer to this will probably turn out to be obvious in retrospect but for now I find myself rather stuck on this. I'll give some blocks of code first and then present the problem.

This is part of my class Stockmanager, I have omitted some methods that have nothing to do with this problem.

import java.util.ArrayList;

public class StockManager
{
private ArrayList stock;

public StockManager()
{
    stock = new ArrayList();
}

public void addProduct(Product item)
{
    stock.add(item);
}

public Product findProduct(int id)
{
    int index = 0;
    while (index < stock.size())
    {
        Product test = stock.get(index);
        if (test.getID() == id)
        {
            return stock.get(index);
        }
        index++;
    }
    return null;
}

public void printProductDetails()
{
    int index = 0;
    while (index < stock.size())
    {
        System.out.println(stock.get(index).toString());
        index++;
    }
}

}

Here's my class Product, again with some methods omitted.

public class Product
{
private int id;
private String name;
private int quantity;

public Product(int id, String name)
{
    this.id = id;
    this.name = name;
    quantity = 0;
}

public int getID()
{
    return id;
}

public String getName()
{
    return name;
}

public int getQuantity()
{
    return quantity;
}

public String toString()
{
    return id + ": " +
           name +
           " voorraad: " + quantity;
}

}

My problem lies in the fact that I get a compile time error in the findProduct() method. To be more specific the line Product test = stock.get(index); is indicated with a message incompatible types.

The constructor of StockManager creates a new ArrayList with the name stock. As is evident from the method addProduct() this ArrayList contains items of the type Product. The Product class has a number of variables one of which is called id and is of type integer. That class also contains a method getID() that returns an id.

As far as I know, the way of getting an item from an arraylist is the get() method with the number between the () indicating the item's position. Seeing as my arraylist contains instances of Product, I expect to get a Product as result when I use the get() method on the arraylist. So I don't understand why it doesn't work when I define a variable called test of the type Product and try to assign an item from the arraylist to it. I have as far as I know, successfully used this same technique in the method printProductDetails() where I use the toString() method from Product on the object from the arraylist.

I hope someone will be able to clarify for me where I am at fault. If it makes any difference, I am doing this stuff in BlueJ which is probably not the best tool for it but it is the one I'm supposed to use for this school project.

Answer

yamafontes picture yamafontes · Oct 21, 2013
private ArrayList stock;

You should redeclare this with a bounded type like so:

private List<Product> stock = new ArrayList<Product>();

If you don't, this line:

Product test = stock.get(index);

won't work because you're trying to assign a raw Object to a Product.

Others have suggested casting the Object to a Product, but I wouldn't recommend this.