James Dawson
James Dawson

Reputation: 5409

Incompatible types error with ArrayList

In my Java class I have this property defined:

private ArrayList requests;

and this is the constructor in the class:

public LeaveRecord() {
    this.requests = new ArrayList<Request>();
    this.daysLeft = ALLOWANCE;
}

in another method I'm trying to return the relevant Request object based on the index of the ArrayList.

public Request getRequestAt(int index) {
    try {
        return this.requests.get(index);
    } catch (IndexOutOfBoundsException e) {
        return null;
    }
}

but I'm getting an error on the return line (line 3 in the above snippet) that says it requires leaverecord.Request but found java.lang.Object.

I don't know what could be wrong, as I defined the ArrayList to be of type Request.

Could someone point me in the right direction? Thanks.

Upvotes: 0

Views: 838

Answers (1)

PermGenError
PermGenError

Reputation: 46438

change it to

private ArrayList requests;

private ArrayList<Request> requests

However compiler should have show you the below warning when you say

 private ArrayList requests;

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

Upvotes: 5

Related Questions