user13735919
user13735919

Reputation:

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

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.

Upvotes: 2

Views: 852

Answers (2)

Jonathan Rosenne
Jonathan Rosenne

Reputation: 2217

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

Upvotes: 3

dcb
dcb

Reputation: 2292

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.

Upvotes: 3

Related Questions