Question

I posted a question yesterday about an issue I was having overriding the toString() for this program, but now I have a different problem. The removeItem() method is supposed to remove the node with the given data value (in this case a String name). I'm getting a NullPointerException on line 64 and I can't seem to figure it out for whatever reason. My code is below, and thanks in advance for any help.

public class StudentRegistration<E>
{
    private static class Node<E> 
    {

        /** The data value. */
        private E data;
        /** The link */
        private Node<E> next = null;

        /**
         * Construct a node with the given data value and link
         * @param data - The data value 
         * @param next - The link
         */
        public Node(E data, Node<E> next) 
        {
            this.data = data;
            this.next = next;
        }

        /**
         * Construct a node with the given data value
         * @param data - The data value 
         */
        public Node(E data) 
        {
          this(data, null);
        }

        public Node getNext()
        {
          return next;
        }

        public E getData()
        {
          return data;
        }

        public void setNext(Node append)
        {
          next = append;
        }
    }
    /** A reference to the head of the list */
    private Node<E> head = null;
    /** The size of the list */
    private int size = 0;

    /** Helper methods */
    /** Remove the first occurance of element item.
    @param item the item to be removed
    @return true if item is found and removed; otherwise, return false.
*/
  public void removeItem(E item)
  {
    Node<E> position = head;
    Node<E> nextPosition1,
            nextPosition2;

    while (position != null)
    {
      if(position.getNext().getData() == item)         //NullPointerException
      {
        nextPosition1 = position.getNext();
        nextPosition2 = nextPosition1.getNext();
        position.setNext(nextPosition2);
      } 
      else
      {
        position = position.getNext();  
      }
    }   
  } 

 /** Insert an item as the first item of the list.
   * @param item The item to be inserted
   */
  public void addFirst(E item) 
  {
      head = new Node<E>(item, head);
      size++;
  }

   /**
     * Remove the first node from the list
     * @returns The removed node's data or null if the list is empty
     */
  public E removeFirst() 
  {
      Node<E> temp = head;
      if (head != null) 
      {
          head = head.next;
      }
      if (temp != null) 
      {
          size--;
          return temp.data;
      } else 
      {
          return null;
      }
  }
  /** Add a node to the end of the list
    *@param value The data for the new node
    */
  public void addLast(E value)
  {
    // location for new value
    Node<E> temp = new Node<E>(value,null);
    if (head != null)
    {
      // pointer to possible tail
      Node<E> finger = head;
      while (finger.next != null)
      {
        finger = finger.next;
      }
      finger.setNext(temp);
    } else head = temp;
  } 

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    Node<E> aux = this.head;
    boolean isFirst = true;
    while(aux != null)
    {
      if(!isFirst)
      {
        sb.append(", ");
      }
      isFirst = false;
      sb.append(aux.data.toString());
      aux=aux.next;
    }
  return sb.append("]").toString();
  } 
}
Was it helpful?

Solution 2

You have an exception when you reach the end and there is no next value. You should be checking like this:

while (position.getNext() != null)

also use equals() instead of == operatoor:

if(position.getNext().getData().equals(item))

OTHER TIPS

Until you are practised in "visualizing" data structures in your head, a good way to understand what is going on is to get a piece of paper and draw a "boxes and pointers" diagram representing the nodes in your data structure (and the relevant fields) ... the local variables in your diagram. Then "hand execute" using a pencil and eraser1.

Don't worry. Linked list insertion and deletion is notoriously tricky for beginners. (That's why it is commonly set as a class exercise in introductory Java and algorithms classes.)


1 - Note careful avoidance of International English faux-pas :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top