Constructor in class cannot be applied to given ty

2019-07-18 23:19发布

问题:

I'm fairly new to Java and I'm using BlueJ. I keep getting the error:

constructor ItemNotFound in class ItemNotFound cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal arguments lists differ in length

I'm fairly confused and in turn not sure how to fix the problem. Hopefully someone can help me. Thank you in advance.

Here is my class Catalog:

public class Catalog {
    private Item[] list;
    private int size;

    // Construct an empty catalog with the specified capacity.
    public Catalog(int max) {
        list = new Item[max];
        size = 0;
    }

    // Insert a new item into the catalog.
    // Throw a CatalogFull exception if the catalog is full.
    public void insert(Item obj) throws CatalogFull {
        if (list.length == size) {
            throw new CatalogFull();
        }
        list[size] = obj;
        ++size;
    }

    // Search the catalog for the item whose item number
    // is the parameter id.  Return the matching object 
    // if the search succeeds.  Throw an ItemNotFound
    // exception if the search fails.
    public Item find(int id) throws ItemNotFound {
        for (int pos = 0; pos < size; ++pos){
            if (id == list[pos].getItemNumber()){
                return list[pos];
            }
            else {
                throw new ItemNotFound(); //"new ItemNotFound" is the error
            }
        }
    }
}

For reference, here is the code for the class ItemNotFound as well:

// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
    public ItemNotFound(int id) {
        super(String.format("Item %d was not found.", id));
    }
}

回答1:

The ItemNotFound class only has one constructor: one that takes an int parameter:

public ItemNotFound(int id)

You're trying to call that without any arguments:

throw new ItemNotFound();

That's not going to work - you need to pass an argument for that parameter. I suspect you just want:

throw new ItemNotFound(id);

(Given that the id parameter to the find method is the ID you're looking for.)

Additionally, I'd recommend that you rename your exception to include a suffix of Exception to follow Java naming conventions - so ItemNotFoundException.

You'll also need to change your loop - currently you're throwing an exception if the first value doesn't have the right ID, whereas presumably you want to loop through all of them. So your find method should look like this:

public Item find(int id) throws ItemNotFoundException {
    for (int pos = 0; pos < size; ++pos){
        if (id == list[pos].getItemNumber()){
            return list[pos];
        }
    }
    throw new ItemNotFoundException(id);
}


回答2:

You provided a custom constructor to your class ItemNotFound and not passing the required arguments when you are using it.

Try to pass required args here

  throw new ItemNotFound(id); 

So your code becomes

public Item find(int id) throws ItemNotFound {
        for (int pos = 0; pos < size; ++pos){
            if (id == list[pos].getItemNumber()){
                return list[pos];
            }
            else {
                throw new ItemNotFound(id); //  Now constructor satisfied
            }
        }
    }

And

throw new ItemNotFound();

That above line is true when your class ItemNotFound is like

// This exception is thrown when searching for an item
// that is not in the catalog.
public class ItemNotFound extends Exception {
    public ItemNotFound() {
        super("Sorry !! No item find with that id"); //Now a generic message.
    }
}


回答3:

You have provided a constructor without any parameters.

public ItemNotFound()

and you are calling new ItemNotFound(id) which while creating instance of class ItemNotFound expects a constructor with one parameter of type int. So you need to have an overloaded constructor

public class ItemNotFound extends Exception {
    public ItemNotFound() {
        super("Sorry !! No item find with that id"); //Now a generic message.
    }

    public ItemNotFound(int if) {
        this(); //Since you are not using any int id in this class
    }
} 


回答4:

throw new Item() is not valid as an explicit constructor has been defined Item(int id).