0

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...

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
barbarossa
  • 129
  • 1
  • 11

3 Answers3

6
ArrayList<Object> menuItems ;

is typed to hold references of Object. It doesn't know what you add at Runtime.

Change it to specific type

List<MenuItem> menuItems = new ArrayList<MenuItem>();
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks, this helped. So, I assume it is not possible for an ArrayList to hold objects of different classes? – barbarossa Oct 14 '17 at 09:03
  • It is possible. You can have something like ` extends SomeBaseClass>`. Read more about [generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html) – jmj Oct 15 '17 at 06:01
0

The problem was this line: ArrayList<Object> menuItems = new ArrayList<Object>();. You're trying to call getName() on an Object, not a MenuItem.

import java.util.*;

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;
  }

  public static void main(String[] args) {
     ArrayList<MenuItem> menuItems = new ArrayList<MenuItem>();

     menuItems.add(new MenuItem("Pizza", 2.22));
     //System.out.println(menuItems);
     System.out.println(menuItems.get(0).getName());
  }
}

Or if you want to keep the list full of Objects, you can explicitly cast the Object to MenuItem when you pull the item out like below:

import java.util.*;

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;
  }

  public static void main(String[] args) {
     ArrayList<Object> menuItems = new ArrayList<Object>();

     menuItems.add(new MenuItem("Pizza", 2.22));
     //System.out.println(menuItems);
     MenuItem mi = (MenuItem) menuItems.get(0);
     System.out.println(mi.getName());
  }
}
Tom O.
  • 5,730
  • 2
  • 21
  • 35
0

Try this statement

System.out.println(((MenuItem)menuItems.get(0)).getName());

(or)

ArrayList<MenuItem> MenuItems = new ArrayList<MenuItem>();
menuItems.add(new MenuItem("Pizza", 2.22));
System.out.println(menuItems.get(0).getName());

You need to typecast it properly before calling the method. Hope this will help u

Abdul
  • 81
  • 8