1

I am trying to display the elements of my ArrayList<LibraryItem> in an action listener method by calling a printlibrary method from another class. I keep getting:

The method printLibrary(ArrayList<LibraryItem>) in the type Library is not applicable for the arguments.

I don't understand what I am doing wrong could you please help me.

import java.util.ArrayList;

public class Library {

    private ArrayList<LibraryItem> items;

    public Library() {
        items = new ArrayList<LibraryItem>();
    }

    public void addItem(LibraryItem newItem) {
        items.add(newItem);
    }

    public LibraryItem searchForItem (String name) {
        for(LibraryItem searchForItem: items) {
            if(searchForItem.getName().equals(name))
                return searchForItem;
        }
        return null;
    }

    public static void printLibrary(ArrayList<LibraryItem> items) {
        for(int i = 0; i < items.size(); i++) {
            System.out.println(items.get(i));
        }
    }
}

public void actionPerformed(ActionEvent ev) {
    String t = title.getText();     
    int y = Integer.parseInt(year.getText());
    int q = Integer.parseInt(quantity.getText());

    library.addItem(new LibraryItem(t,y,q)); 

    library.printLibrary(); -------->>>> ERROR HERE!!!
}                                                                                                     this is the edited version and the error                                               public void printLibrary(ArrayList<LibraryItem> items) {
        for(int i = 0; i < items.size(); i++) {
            System.out.println(items.get(i));
        }
    }
}      public void actionPerformed(ActionEvent ev) {
    String t = title.getText();     
    int y = Integer.parseInt(year.getText());
    int q = Integer.parseInt(quantity.getText());

    library.addItem(new LibraryItem(t,y,q)); 

    library.printLibrary(); 
}                                                                                              this is my code for libraryitem class `                                                                public class LibraryItem 

{

    private String name;
    private int year, quantity;

   LibraryItem(String nameIn, int yearIn, int quantityIn)
   {
       name = nameIn;
       year = yearIn;
       quantity = quantityIn;



   }



public boolean rent()
{


    if(quantity > 0) 

    {
         quantity--;

    }
      return true;
}






    public String toString()
   {
return name + " " +"has"+ " " +quantity+ "books on loan";


   }

    public String getName()
   {
return name;

   }


   public int getQuantity()
   {

   return quantity;
   }

   public int getYear()
   {

   return year;
   }


   }

`

this is my code for library ` import java.util.ArrayList;

 public class Library {

private ArrayList<LibraryItem> items;




public Library()
{
    items = new ArrayList<LibraryItem>();
}



public void addItem(LibraryItem newItem)
{
    items.add(newItem);

}

public LibraryItem searchForItem (String name)

{
    for(LibraryItem searchForItem: items)
    {
        if(searchForItem.getName().equals(name) )

    return searchForItem;


}
    return null;
}

public void printLibrary()
{


    for(int i = 0; i< items.size(); i++)
    {

        System.out.println(items.get(i));
    }


}

}

                                   `                                                                                                                                           
2
  • How is your printLibrary method declared? Does it accept any arguments? Also, why is it static? Commented Jan 15, 2014 at 16:06
  • Your printLibrary method has a parameter of ArrayList type. But in your action listener your calling it with no arguments. Can you explain why? Commented Jan 15, 2014 at 16:11

2 Answers 2

2

Look at your call:

library.printLibrary();

Now look at the declaration:

public static void printLibrary(ArrayList<LibraryItem>items)

This is a static method, with a parameter which is a list. You're trying to call it as an instance method (which Java allows even when calling a static method, unfortunately, but which should be avoided anyway) but not supplying any arguments.

Given that Library already has a list of items, I suggest you just change the declaration to make it a parameterless instance method:

public void printLibrary()

... leaving the body of the method the same, which will then pick up the instance variable items.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your help that has seemed to get rid of my error! but the elements of my array list are still not being displayed when click to add an element to my array list via my interface. I get these messages. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at LibraryFrame.actionPerformed(LibraryFrame.java:62) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
@user3198936: Now that you've included the message... which is line 62? Where are you declaring library and how are you initializing it?
public class LibraryItem { private String name; private int year, quantity; LibraryItem(String nameIn, int yearIn, int quantityIn) { name = nameIn; year = yearIn; quantity = quantityIn; } public boolean rent() { if(quantity > 0) { quantity--; } return true; } public String toString() { return name + " " +"has"+ " " +quantity+ "books on loan"; } public String getName() { return name; } public int getQuantity() { return quantity;} public int getYear() { return year; } }
@user3198936: Please don't dump large amounts of code in comments. Edit the question instead. Although to be honest this should all be in a new question now - I've answered the question you asked. The next problem is a different one.
I have now edited the question and posted my code for library
|
0

You have declared printLibrary(ArrayList<LibraryItem> items), yet you call it without passing in any arguments.

The declaration:

public static void printLibrary(ArrayList<LibraryItem>items)
{
    for(int i = 0; i< items.size(); i++)
    {
        System.out.println(items.get(i));
    }
}

And the call to it:

library.printLibrary();

The call to the printLibrary() method requires an ArrayList, so instead you would want to call it like:

library.printLibrary(items);

However, you should change how the method is declared if all you are doing is printing out a information from a private data member of the class. A better declaration would be:

public void printLibrary()
{
    for(int i = 0; i< items.size(); i++)
    {
        System.out.println(items.get(i));
    }
}

Now, your library's printLibrary() method will print each item in your items data member. If you change your declaration to this, you can leave the call as library.printLibrary().

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.