Search code examples
javabluej

Java Incompatible Types Error Arraylists


I'm using BlueJ as an IDE and whenever I try to compile this Java code it gives me an error: incompatible types highlighting the brackets of:

s.getCourtSportArrayList() 

Why this is happening?

public void showCourtBookings()
{
 for(Sport s : sportList)
 {
   for(Court c : s.getCourtSportArrayList() )
   {
     System.out.println("Court: " + c.getCourt);
     int i;
     i=1;
     for(Booking b : c.getBookings())
     {  
         System.out.println("Booking: " + i + "Start Time: " + b.getTimeStart() + "End Time :" + b.getEndTime());
         i = i + 1;
     }
   }
 }  
}

This is a class Club, it contains two ArrayLists;

private ArrayList<Member> MemberList;
private ArrayList<Sport> sportList;

The Sport class has the following ArrayList:

private ArrayList<Court> CourtList = new ArrayList<Court>();

The Court class has these ArrayLists:

private ArrayList<Booking> listBooking;

Hopefully you can point me in the right direction. Thanks!

Edit: this is the code,

public ArrayList getCourtSportArrayList()
{
  return CourtList;
}

Solution

  • The getCourtSportArrayList() seems to be a method of your Sport class. This method needs to return a List<Court> which it apparently does not right now.

    in result the method should look like this:

    public List<Court> getCourtSportArrayList()
    {
       return CourtList;
    }
    

    Side note: You should specify the generic Type List<Court> instead of ArrayList<Court> unless you use specific implementation details of ArrayList.