0

So here I have created a PolyLineIterator for my object PolyLine. Here is the code for the iterator

 public class PolyLineIterator {
   private int  current = -1;
   public PolyLineIterator() {
       if(Polyline.this.vertices.length > 0 ) {
           current = 0;
       }
   }
   public boolean hasVertex() {
       return current != -1;
   }
   public  Point vertex() throws java.util.NoSuchElementException {
       if(!this.hasVertex())throw  new  java.util.NoSuchElementException("end of iteration");
       Point vertex = Polyline.this.vertices[current];
       return vertex;
   }
   public void advance() {
       if(current >= 0 && current < Polyline.this.vertices.length - 1) {
           current ++;
       }else {
           current = -1;
       }
   }

}

When I am done typing the code an error pops up underlining Polyline and it says the following:-

No enclosing instance of the type Polyline is accessible in scope

How do I resolve this issue?

shahaf
  • 4,750
  • 2
  • 29
  • 32
  • 1
    Your class is not a nested class in Polyline. https://stackoverflow.com/questions/5439241/no-enclosing-instance-of-the-type-is-accessible-in-scope – radulfr Nov 16 '19 at 11:37
  • What's the relationship between `PolyLineIterator` and `Polyline`? And why are you using the `Polyline.this.vertices` construct? – ernest_k Nov 16 '19 at 11:44
  • I have to create an instance of PolyLineIterator in the test program for class Polyline and I have to visit each vertex in sequence and print them. –  Nov 16 '19 at 12:48
  • It seems you have copied and pasted this code from somewhere without understanding that `Polyline.this` can only be used if the iterator is an inner class of PolyLine. https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html – JB Nizet Nov 16 '19 at 13:02
  • It is from an assignment that i have posted the code. –  Nov 16 '19 at 13:09

0 Answers0