0

So i am working on an implementation of a singly linked list and i have run into the dreaded null pointer exception. Here's my code:

class Link
{
   public Object data;
   public Link next;
}

class List
{
   private Link _head;
   private Link _tail;
   private int counter = 0;

   public List()
   {
      _head = null;
      _tail = null;
   }

   public void insert(Object item, int pos)
   {
      if (pos == counter)
      {
         insertAtEnd(item);
         return;
      }

      Link temp = _head;
      for (int i = 1; i < pos; ++i)
      {
         temp = temp.next;
      }

      Link added = new Link();
      added.data = item;
      added.next = temp.next;
      temp.next = added;
      ++counter;
   }

   public void insertAtEnd(Object item)
   {
      if (_head == null)
      {
         Link added = new Link();
         added.data = item;
         added.next = _tail;
         _head = added;
      }
      else
      {
         _tail.next = new Link();
         _tail = _tail.next;
         _tail.data = item;
      }
      ++counter;

   }

   public void deleteRange(int start, int end)
   {
      Link temp = _head;
      Object deleted;

      for (int i = 1; i < start - 1; ++i)
      {
         temp = temp.next;
      }
      for (int i = start; i < end; ++i)
      {
         deleted = temp.next.data;
         temp = temp.next;
         --counter;
      }
      temp.next = temp.next.next;
   }

   public void deleteAll(Object item)
   {
      Link temp = _head;
      Link temp2 = _head;
      Link prev = null;
      Object deleted;
      int times = 0;

      for (int i = 1; i <= counter; ++i)
      {
         if (temp.data.equals(item))
            ++times;
         temp = temp.next;      
      }

      for (int i = 1; i <= counter; ++i)
      {
         prev = temp2;
         if (temp2.data.equals(item))
         {
            deleted = temp2.data;
            temp2 = prev;
         }
         temp2 = temp2.next;

      }
      counter = counter - times;
   }

   public Object retrieve(int pos)
   {
      Link temp = _head;

      for (int i = 1; i < pos; ++i)
      {
         temp = temp.next;
      }
      return temp.data;
   }

   public List find(Object item)
   {
      Link temp = _head;
      Link prev = null;
      int count = 0;
      boolean found = false;
      List positions = new List();
      positions.insertAtEnd((Integer) count);
      return positions;

   }

   public int getSize()
   {
      return counter;
   }

   public String toString()
   {
      Link temp = _head;
      String s = "";
      while (temp.next != null)
      {
         s += temp.data + ", ";
         temp = temp.next;
      }   
      return s;   
   }

When calling the toString method to test my code, the null pointer exception pops up. I think my problem lies in my insertion methods but i am not completely sure. I am fairly new to programming and i just can't fully wrap my head around the singly linked list dynamic data structure

Thanks in advance!

  • 1
    Can you catch the exception and add the stack trace to your question? – dave Apr 27 '15 at 09:31
  • 1
    You check whether `temp.next` is null, but note that `temp` can also be null, because `_head` is initialised as null. – DNA Apr 27 '15 at 09:32
  • 1
    What will the value of `_head` be if you haven't inserted into your list yet? – JonK Apr 27 '15 at 09:32
  • Edit on previous comment: The ```toString()``` of list should simply add ```temp.toString()``` to the list, and the ```Link``` object should have a ```toString()``` that isolates what is printed -- the ```data``` field. – Brian Topping Apr 27 '15 at 09:35
  • @JonK can you develop more into that please? – Gera Pedraza Apr 27 '15 at 09:36
  • Nowhere in your code do you assign `_head` to anything other than `null`. – Misha Apr 27 '15 at 09:42
  • @Misha I think i fixed it, but my tostring is still not returning anything – Gera Pedraza Apr 27 '15 at 10:03
  • @GeraPedraza I think your `while` loop in `toString()` should have the condition `temp != null`. – dave Apr 27 '15 at 10:55

1 Answers1

0

I think, in your function InsertAtEnd(), you didn't initialize _head in your first if condition.