Question

I am trying to access a specific element in a java list of Greenfoot (http://www.greenfoot.org/).

getObjects(Object.class).get(0).getPosition();

Object is my own class. It has a method named public float getPosition() { ... }

getObjects() returns a java.util.List. Documentation of Greenfoot -> World: http://www.greenfoot.org/files/javadoc/

But the error is: cannot find symbol - method getPosition()

Was it helpful?

Solution

As per the documentation:

Get all the objects in the world, or all the objects of a particular class.

If a class is specified as a parameter, only objects of that class (or its subclasses) will be returned.

Since you have passed the Class as Object.class. It is returning a List of java.lang.Object class and hence you get that error .You need to rename your class.

((yourClassName)getObjects(yourClassName.class).get(0)).getPosition();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top