How, if it’s possible, can you call a method from another method’s return?
For example…
class Example {
public static void main(String[] args) {
Point t1 = new Point(0,0);
Point t2 = new Point(0,1);
ArrayList pointContainer = new ArrayList();
pointContainer.add(0,t1);
pointContainer.add(0,t2); // We now have an ArrayList containing t1 & t2
System.out.println(pointContainer.get(1).getLocation()); // The problem area
}
}
In the poorly written example, I’m trying to invoke the getLocation() method (part of java.swing.awt) on index item 1 of pointContainer.
When trying to compile the program, I get the following error…
HW.java:20: error: cannot find symbol
System.out.println(test.get(1).getLocation());
^
symbol: method getLocation()
location: class Object
Could someone please help me with this problem.
First, type your ArrayList so that Java can know what objects are coming out of it.
Then, any objects you retrieve from that ArrayList will be of type
Point, so you can perform operations on them.