Mutating Algorithm
Mutating Algorithm

Reputation: 2758

Expected class found object

ArrayList transactions = new ArrayList<Transaction>();

public void printTransactions() {
    for(Transaction t: transactions)
        System.out.println("Transaction Type: " + t.getType() +
                           "\nDescription: " + t.getDescription() +
                           "\nAmount: " + t.getAmount() +
                           "\nNew Balance: " + t.getBalance() + 
                           "\nDate: " + t.getDate());
}

I'm new to Java and I have spent quite some time trying to figure out this error. In my printTransactions() method, it says starting with the for loop line "Error: Incompatible types required Transaction found Object"

What I have tried is cast t to a transaction class Transaction(t).getType() but that has not helped either.

Upvotes: 0

Views: 684

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24423

You are missing the generic type in transactions declaration. Try this

ArrayList<Transaction> transactions = new ArrayList<Transaction>();

Upvotes: 6

Related Questions