Home  >  Forum  >  Core Java
Post New Query

"main" java.lang.Error: Unresolved compilation problems



Asked on March 21, 2015
While learning the polymorphism in java I have created a list MyAnimalList and I want to add Animals in the list like Dog and Cat. The size of list is 5 but when I compile it, it throw some exception.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Cat cannot be resolved to a type
Cat cannot be resolved to a type
The method add(Animal) in the type MyAnimalList is not applicable for the arguments (Dog)

AnimalTestDrive.java

class MyAnimalList {
private Animal [] animals = new Animal[5];
private int nextIndex = 0;
public void add(Animal a) {
if(nextIndex < animals.length){
animals[nextIndex] = a;
System.out.println("Animal added at " + nextIndex);
nextIndex++;
}
}
}

public class AnimalTestDrive {
public static void main(String[] args) {
MyAnimalList list = new MyAnimalList();
Dog a = new Dog();
Cat c = new Cat();
list.add(a);
list.add(c);
}

}

Can any one short out the problem? 




Replied on March 22, 2015
Error says that, there is compilation error in your code. According to the error message

Cat cannot be resolved to a type
Cat cannot be resolved to a type

Cat class is not accessible, it may be because

1. You have not imported the class Cat or
2. You don't have any such class.






Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us