I'm very new to Java and might not see sth obvious, but here's my problem:
For some reason I cannot access the methods of my class MenuItem
:
public class MenuItem {
String name;
double price;
public MenuItem (String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return this.name;
}
public String toString() {
return this.name + "\t" + this.price;
}
}
In the following code snippet my IDE underlines the method getName()
red and says:
"cannot find symbol"
This is the code:
public static void main(String[] args) {
ArrayList<Object> menuItems = new ArrayList<Object>();
menuItems.add(new MenuItem("Pizza", 2.22));
System.out.println(menuItems.get(0).getName());
}
I compared my code with code of other's and cannot find any difference.
Somehow the toString()
method works perfectly fine. It returns this.name + "\t" + this.price;
and not filechallenge.MenuItem@15db9742
which it does when I delete the method.
Does anybody have an idea? I really don't get it...