You want to make sure you're assigning the tree variable within your loop and not just inside your class, because otherwise it will never get reassigned.
Also, your code will currently get the closest tree to the player which may not be in the tree area, so one way to only get trees within the tree area is using Filters. Here is an example of an RS2Object filter where I'm filtering for three things:
Area draynorArea = new Area(3075, 3249, 3102, 3214);
public static Filter<RS2Object> WILLOW = new Filter<RS2Object >() {
@Override
public boolean match(RS2Object object) {
return object.getName().equals("Willow") && object.hasAction("Chop down") && draynorArea.contains(object);
}
};
Hopefully it's clear based on the code, but this filters for objects named "Willow" that have the action "Chop down" that are contained in my area variable of draynorArea. Then making use of this would be:
RS2Object myTree = objects.closest(WILLOW);
Now the variable myTree will only get the closest object that fits our filter. Best of luck!