Error shows Method is undefined when I clearly have it written in the class it is referring to, in Java?

Viewed 263

I have removed good chunks of my code as it is irrelevant to this issue. This is my TRAPSCalendar class:

import java.awt.Event;
import java.util.ArrayList;

public class TRAPSCalendar {
    private ArrayList<Event> calendar;
    
    public TRAPSCalendar() {
        calendar = new ArrayList<Event>(); //create object
    }
    
    public Event get(java.lang.String name) { // the list of event names 
        for (int i = 0; i < calendar.size(); i++) {
            if (calendar.get(i).getEventName().equals(name)) {  // getEventName() from Event.java
                return calendar.get(i);
            }
            
        }
        return null;
    }
    
}

There is an error at if (calendar.get(i).getEventName().equals(name))... The method getEventName() is undefined for the type Event.

However, in my Event.java, I have:

import java.time.format.DateTimeFormatter;
import java.time.LocalDate;

public class Event {
    String eventName;
    String date;
    String eventVenue;
    int venueCapacity;
    int ticketsSold;
    int ticketPrice;
    int overhead;

    public Event() {
        //create
    }
//other methods for other things
    public String getEventName() {
        return eventName;
    }
}

I don't know what I'm doing wrong and it continues to show the error.

2 Answers

In the first line of TRAPSCalendar, you import java.awt.Event; This is why you are not working with your own Event class, but with java.awt.Event. The error message comes up because there is no java.awt.Event.getEventName() method implemented.

In order to fix this issue, remove the import java.awt.Event; statement and import your own Event class instead. That should do the trick.

You are using java.awt.Event rather than your own Event.

Related