Results 1 to 9 of 9
  1. #1
    ztrath is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Question [SOLVED] Linked list question

    Hi everyone, I have a question.
    Im taking a java class and I was assigned a project assignment and i cant figure out where im going wrong at. its a simple linked list project. i submitted my code and received the following error:
    symptom: java.lang.NullPointerException
    at LinkedPriorityList.getElementAt
    at first i thought it was in my getElementAt, and also checked the node code, but no luck. i have a long list of test cases that i thought covered everything, but i guess im wrong. the rest of the code is done, but i just cant figure out what my teacher is testing to get that result.
    Thanks Jeff

    Java Code:
    /**
     * implement the same PriorityList ADT from 6A with class LinkedPriorityList.
     * This project has the following goals: <li>Implement an ADT using a different
     * data structure  <li>Understand how generic classes
     * safely store collections of the same type element <li>Use exceptions to
     * handle out of range indexes <li>Implement interfaces
     * <p>
     * A PriorityList stores a collection of elements as a zero-based indexed list
     * where the element at index 0 is considered to have higher priority than the
     * element at index 1. The element at index size()-1 has the lowest priority. A
     * PriorityList stores a collection of just one type of element that can be any
     * type.
     */
    public class LinkedPriorityList<E> implements PriorityList<E> {
    
    	/**
    	 * This private inner class saves lots of typing and is hidden from
    	 * outsiders
    	 */
    	private class Node {
    
    		private E data;
    		private Node next;
    
    		 public Node(E element) {
    		 data = element;
    		 next = null;
    		 }
    		
    		public E getData() {
    			return data;
    		}
    
    		public Node(E element, Node link) {
    			data = element;
    			next = link;
    		}
    	}
    
    	private Node first;
    	private int size;
    
    	public LinkedPriorityList() {
    		first = null;
    		size = 0;
    	}
    
    	public void insertElementAt(int index, E el)
    			throws IllegalArgumentException {
    		if (index > size || index < 0)
    			throw new IllegalArgumentException();
    		Node ref = first;
    		if (index == 0) {
    			if(size==0){
    			Node temp = new Node(el);
    			first = temp;
    			}else{
    				Node temp = new Node(el, first);
    				first = temp;
    				}
    
    		} else {
    			for (int count = 0; count < index - 1; count++, ref = ref.next)
    				;
    			Node temp = ref.next;
    			ref.next = new Node(el, temp);
    		}
    		size++;
    	}
    
    	public E getElementAt(int index) throws IllegalArgumentException {
    		if (index >= size || index < 0 || size == 0)
    			throw new IllegalArgumentException();
    	
    
    		int count = 0;
    		Node ref = first;
    		while (count < index) {
    			count++;
    			ref = ref.next;
    		}
    
    		return ref.getData();
    	}
    
    	public boolean isEmpty() {
    		return size == 0;
    	}
    
    	public void lowerPriorityOf(int index) throws IllegalArgumentException {
    		if (index >= size || index < 0)
    			throw new IllegalArgumentException();
    		Node ref = first;
    		E temp;
    		if (index < size - 1) {
    			for (int count = 0; count < index; count++, ref = ref.next)
    				;
    			temp = ref.data;
    			ref.data = ref.next.data;
    			ref.next.data = temp;
    		}
    	}
    
    	public void moveToLast(int index) throws IllegalArgumentException {
    		if (index >= size || index < 0)
    			throw new IllegalArgumentException();
    		for (int i = index; i < size - 1; i++)
    			lowerPriorityOf(i);
    
    	}
    
    	public void raisePriorityOf(int index) throws IllegalArgumentException {
    		if (index >= size || index < 0)
    			throw new IllegalArgumentException();
    		Node ref = first;
    		E temp;
    
    		for (int count = 0; count < index - 1; count++, ref = ref.next)
    			;
    		if (index > 0) {
    			temp = ref.data;
    			ref.data = ref.next.data;
    			ref.next.data = temp;
    		}
    	}
    
    	public void moveToTop(int index) throws IllegalArgumentException {
    		if (index >= size || index < 0)
    			throw new IllegalArgumentException();
    		for (int i = index; i > 0; i--)
    			raisePriorityOf(i);
    	}
    
    	public void removeElementAt(int index) throws IllegalArgumentException {
    		if (index >= size || index < 0)
    			throw new IllegalArgumentException();
    		if (index == 0 || size == 1) {
    			first = null;
    		} else {
    			Node ref = first;
    
    			for (int count = 0; count < index - 1; count++, ref = ref.next)
    				;
    
    			ref.next = ref.next.next;
    		}
    		size--;
    
    	}
    
    	public int size() {
    		return size;
    	}
    
    	public Object[] toArray() {
    		Node ref = first;
    		Object[] result = new Object[size];
    
    		for (int i = 0; i < size; i++) {
    			result[i] = ref.data;
    			ref = ref.next;
    
    		}
    
    		return result;
    
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you indicate which line in the code above throws the NPE?

  3. #3
    ztrath is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    nope, i think its someplace in getElementAt(). all i know is that when i pitch it up against his code, that's the error it gives back. I think the code is well protected from someone putting in a bad input into getElementAt().

    P.S. i know that IllegalArgumentException is the wrong error to throw, but its the one our teacher wants us to throw

    Jeff

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ztrath View Post
    nope
    Oh, come on now. The exception stack trace should give out the line number.

  5. #5
    ztrath is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    Because the program he uses, it dosent give us the full error, it only gives us part of the error as a hint

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    OK. In this method here:
    Java Code:
      public E getElementAt(int index) throws IllegalArgumentException
      {
        if (index >= size || index < 0 || size == 0)
          throw new IllegalArgumentException();
    
        int count = 0;
        Node ref = first;
        while (count < index)
        {
          count++;
          ref = ref.next;
        }
    
        return ref.getData();
      }
    there are only two places where you dereference an object. One is ref.next and the other is ref.getData(). The NPE is being thrown at one of these two spots. My suspicion is that it's the latter of the two, the ref.getData(). Now why this is happening, I don't know, not without being able to test the code some.

    edit: but... you already know this. I'm stumped without a stack trace or the testing conditions that are throwing this error. On my inspection of your code I fail to find any errors that would cause an NPE, but that's not worth much. Again, I'm stumped.
    Last edited by Fubarable; 03-10-2009 at 12:45 AM.

  7. #7
    ztrath is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    I added ref.getData() to try and fix the problem, ref.data will work just as well, but i was trying different things. And i think im stumped at the same location as you. i have no clue where a NPE could come from

  8. #8
    ztrath is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    So for anybody that has this same problem after me, here is the correct code:
    public void removeElementAt(int index) throws IllegalArgumentException {
    if (index >= size || index < 0)
    throw new IllegalArgumentException();

    Node ref = first;

    for (int count = 0; count < index - 1; count++, ref = ref.next)
    ;

    if(index==0 && size > 0)
    first = first.next;

    if(index >0)
    ref.next = ref.next.next;

    size--;

    }
    the code is about the same, but i guess the slight difference makes it.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  2. Revised Linked List printing method question
    By CirKuT in forum New To Java
    Replies: 7
    Last Post: 12-12-2008, 11:21 PM
  3. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  4. Linked List Question
    By CirKuT in forum New To Java
    Replies: 4
    Last Post: 12-10-2008, 07:56 PM
  5. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •