1
import java.util.*;

public class GameMain{

   public static void main(String [] args){
      Scanner scan = new Scanner(System.in);
      Random rand = new Random();
      Enemy e1 = new Knight();
      Enemy e2 = new Skeleton();
      Enemy e3 = new Demon();
      ArrayList<Enemy> enemyArray = new ArrayList<Enemy>(); 
      enemyArray.add(e1);
      enemyArray.add(e2);
      enemyArray.add(e3);
   }



  public static void enterCastle(ArrayList enemyArray, Random rand){   

       int enemy = rand.nextInt(2);
       String name = enemyArray.get(enemy).getName();
       System.out.println();
       System.out.println("You enter the castle");
       System.out.println("Darkness are closing on you!");
       System.out.println("You hear something from behind you");
       System.out.println("you look behind you and see a "+enemyArray.get(enemy).getName());


       }

Error:

GameMain.java:51: error: cannot find symbol
   String name = enemyArray.get(enemy).getName();
                                      ^
  symbol:   method getName()
  location: class Object
GameMain.java:56: error: cannot find symbol
   System.out.println("you look behind you and see a "+enemyArray.get(enemy).getName());
                                                                            ^
  symbol:   method getName()
  location: class Object
2 errors

I am trying to make a method that pick a random enemy from super class which is in a ArrayList called enemyArray.

when i try to call the getName method from super class i get the error in the bottom.

can any of you pros tell me what i am doing worrng?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

3
public static void enterCastle(ArrayList enemyArray, Random rand)

This declares enemyArray as a list of Objects. The generic parameter is missing. When you call enemyArray.get(enemy), the result is an Object, and Object doesn't have a getName() method, thus the error.

public static void enterCastle(ArrayList<Enemy> enemyArray, Random rand)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2

Well lets assume your code is something like this :

class Enemy{
    public String getName(){
    // bla bla
    return "hey";
    }
}
class Knight extends Enemy{

}
class Skeleton extends Enemy{

}
class Demon extends Enemy{

}

Just change your method signature of enterCastle method as :

public static void enterCastle(ArrayList<Enemy> enemyArray, Random rand)

Reason for the error : As you are not mentioning the Generic type, Java assumes to take it as Object and hence cannot find the method.

  • The whole *"Well lets assume"* part with the first code block is not really important for your answer and the solution to OPs issue. You can think about removing that. – Tom Nov 16 '16 at 14:16
  • @Tom, I've used it just because if a beginner level guy comes across this question, he/she might worry about the classes that were being used and the relationship among them. May be it can help someone... – Satya Dileep Kumar Nov 22 '16 at 14:07