title: Visitor returns first Item of an List as null
body: <p>I want to implement a Visitor for My class <code>MyList</code>. The List itself holds Elements of the type <code>MyEntry</code>. Those Entrys hold a generic Value and a reference to the next Entry in the List. </p>
  
  <pre><code>public class MyEntry&lt;E&gt; implements Visitable {
  
      MyEntry&lt;E&gt; next;
      E o;
  
      MyEntry() {
         this(null, null);
      }
  
      MyEntry(E o) {
        this(o, null);
      }
  
      MyEntry(E o, MyEntry&lt;E&gt; e) {
       this.o = o;
       this.next = e;
      }
  
      public void accept(Visitor visitor) {
      }
  }
  </code></pre>
  
  <p>The List class </p>
  
  <pre><code>public class MyList&lt;E&gt; implements Visitable {
  
   private MyEntry&lt;E&gt; begin;
  
   private MyEntry&lt;E&gt; pos;
  
  
   public MyList() {
     pos = begin = new MyEntry&lt;E&gt;();
   }
  
  
   public void add(E x) {
     MyEntry&lt;E&gt; newone = new MyEntry&lt;E&gt;(x, pos.next);
  
     pos.next = newone;
   }
  
  
   public void advance() {
      if (endpos()) {
         throw new NoSuchElementException("Already at the end of this List");
      }
      pos = pos.next;
   }
  
  
   public void delete() {
      if (endpos()) {
         throw new NoSuchElementException("Already at the end of this List");
      }
      pos.next = pos.next.next;
   }
  
  
   public E elem() {
      if (endpos()) {
         throw new NoSuchElementException("Already at the end of this List");
      }     
      return pos.next.o;
   }
  
  
   public boolean empty() {
      return begin.next == null;
   }
  
  
   public boolean endpos() { 
      return pos.next == null;
   }
  
   public void reset() {
      pos = begin;
   }
  
    @Override
    public void accept(Visitor visitor) {
  
        begin = pos;
  
        while(pos.next != null &amp;&amp; visitor.visit(pos.o)) {
            //Move one Item forward
            pos = pos.next;
  
       }
  }
  </code></pre>
  
  <p>I have implemented the <code>Visitor</code> interface to my <code>ListVisitor</code> class</p>
  
  <pre><code>public class ListVisitor implements Visitor {
  
    public ListVisitor() {
        }
  
    @Override
    public boolean visit(Object o) {
        return false;
    }
  }
  </code></pre>
  
  <p>The Interface <code>Visitor</code> that will be implemented in every new Visitor that i want to create, for now i only have the <code>ListVisitor</code></p>
  
  <pre><code>public interface Visitor&lt;E&gt; { 
     boolean visit(Object o);
  }
  </code></pre>
  
  <p>And the Interface Visitable, that is implemented in every class i want to visit, in this case the MyList class and the MyEntry class.</p>
  
  <pre><code>public interface Visitable {
      public void accept(Visitor visitor);
  }
  </code></pre>
  
  <p>To test the implementation of the Visitor pattern I made a Testclass that creates a new Mylist and puts some Strings in it. Next it creates a new Visitor that visits the List. </p>
  
  <pre><code>import org.junit.Test;
  
  public class MyListTest {
  
    @Test
    public void MyListTest() {
  
        MyList s = new MyList();
  
  
       s.add("Hello");
       s.add("World");
       s.add("!");
  
        Visitor v = new Visitor() {
  
            @Override
            public boolean visit(Object o) {
              System.out.println(o);
              return true;
            }
        };
        s.accept(v);
     }
  }
  </code></pre>
  
  <p>Now when I run MyListTest the output is:<br>
  null<br>
  !<br>
  World<br></p>
  
  <p>My Question now is why the first Element that the Visitor visits has a null reference in it. When i add more items to my List before creating a Visitor the output always extends, except for the first item that has been inserted into the List, it will always be null.</p>