title: Get Method, Insert, and Remove At Methods For a Custom Linked List
body: <p>I am having a great deal of trouble trying to implement these three methods in a custom Linked List I am creating. The get(int pos) method will return null for everything but the 0th index even when I have an existing Linked List! My removeAt(int pos) and insert(int pos, int n) methods are having the same trouble. I'll include everything I believe is relevant.</p>
  
  <p><strong>My Node Class</strong></p>
  
  <pre><code>    public class MyNode 
     {
        public int cargo;
        public MyNode next;
  
        public MyNode()
        {
          cargo = 0;
          next = null;
        }
  
        public MyNode(int i)
        {
          cargo = i;
          next = null;
        }
     }
  </code></pre>
  
  <p><strong>My Custom Linked List</strong></p>
  
  <pre><code>public class MyLinkedList 
  {
  MyNode head;
  int size;
  
  public MyLinkedList()
  {
      head = null;
      size = 0;
  }
  
  public int get(int pos)
  {
      MyNode current = new MyNode();
      for(int i = 0; i &lt;= pos &amp;&amp; current != null; i++)
      {
          if(pos == 0){
              current = head;
          }else{
              current = current.next;
              break;
          }
      }
      return current.cargo;
  }
  
  public void insert(int pos, int n)
  {
      MyNode current = new MyNode(n);
      MyNode before = new MyNode(get(pos-1));
      if(before.next != null)
      {
          before.next = current.next;
      }
      else
      {
          before.next = current;
      }
      size++;
      System.out.println("Inserted " + n + " at position " + pos);
  }
  public void removeAt(int pos)
  {
      MyNode before = new MyNode(get(pos -1));
      MyNode remove = new MyNode(get(pos));
      MyNode after = null;
  
      int cargo = remove.cargo;
  
      if(remove.next != null)
      {
          after = remove.next;
      }
      before.next = after;
      size--;
      System.out.println("Removed " + cargo + " from position " + pos);
  }
  }
  </code></pre>
  
  <p><strong>My Test Data</strong></p>
  
  <pre><code>l.add(17);
  l.add(14);
  l.add(43);
  l.add(50);
  System.out.println(l.get(1));
  </code></pre>
  
  <p>returns a NullPointer Exception at the sysout</p>