I am testing my Library class and the addItem method is not adding an item to the arraylist

import java.util.ArrayList;

public class Library {

     /**
     *  itemList contains the List of all items in the library.
     */
     private ArrayList<Item> itemList = new ArrayList<Item>();
     Item item = new Item();
     // Empty Constructor
    public Library(){}

    /** 
     * Add a new item to the list of library items. 
     * @param newItem The new item to be added to the list.
     */
    public void addItem(Item newItem)
    {
        itemList.add(newItem);
    }

The testDriver has this method

Library str = new Library();
ArrayList itemList = new ArrayList();
Item item = new Item();
Item cd = new MusicCD("id002", "Best of Police", false, null, 5.50, "Sting", "Sting");
str.addItem(cd);
str.loanItem("id002", null);

When I ask it to display the arraylist itemList it has nothing in it

System.out.println(itemList.size());

I am not sure where I am going wrong here?

Driver line 2 is the error. It means you have two lists where you should only have one.
You have a list in your library class, you don’t need another one in the driver.
You add the item to the list in Library, then print the list in Driver - which is still empty.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.