Click here to Skip to main content
12,072,809 members (67,213 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
This circular link list compiles but when run it gives me this exception.
I am sorry my English is not very good

Exception in thread "main" java.lang.NullPointerException
at MyCircularLinkedList.getNode(MyCircularLinkedList.java:123)
at MyCircularLinkedList.add(MyCircularLinkedList.java:67)
at MyCircularLinkedList.add(MyCircularLinkedList.java:59)
at TestLinkedList.main(MyCircularLinkedList.java:259)

here is my code:

public class MyCircularLinkedList<AnyType> implements Iterable<AnyType>,
MyList<AnyType> {
 
	
	private int theSize;
	private int modCount = 0;
	private Node<AnyType> endMarker;
 
	/**
	 * Construct an empty LinkedList.
	 */
	public  MyCircularLinkedList(){                
		
		clear();
	}
 
	public void clear() {
		
		endMarker = null;
		theSize =0;
		modCount++;
		
		
	}
 

	public int size() {
		
		return theSize;
	}
 
	public boolean isEmpty() {
		
		
		return size() ==0;
		
	}
	
	public AnyType peek()
	{	
		Node<AnyType> nodo2;
		nodo2 = endMarker.next;
		return nodo2.data;
		
	}
 
public AnyType tail()
{
	Node<AnyType> nodo1;
	nodo1 = endMarker.next;
	for(int i =0;i< theSize ;i++)
		nodo1 =nodo1.next;
	return nodo1.data;
	
}
	
	public boolean add(AnyType x) {
		add(size() -1,x); // this is line 59 3rd error
		return true;
 
	}
 
	
	public void add(int idx, AnyType x) {
	
		addAfter(getNode(idx, 0 ,size()),x); //this is line 67 sec error
		
		
	}
 
	
	 
	private void addAfter(Node<AnyType> p, AnyType x)
	{
	if (theSize == 0) {
		
		//agregar un nodo a la lista 
		
	} else 
	{
		
		// to -do
		
	}
		theSize++;
		modCount++;
		
		
	}
 
	
	public AnyType get(int idx) {
		
		return getNode(idx).data;
	}
 
	
	private Node<AnyType> getNode(int idx) {
		
		if(theSize ==0) {
			return null;
			
		}
		
		return getNode(idx,0,size()-1);
	}
 
	private Node<AnyType> getNode(int idx, int lower, int upper) {
		
		if(theSize ==0)
		{
			return null;
			
			
			
		}
		
			Node<AnyType> p;
			if(idx < lower || idx> upper)
			throw new IndexOutOfBoundsException("getNode index : " + idx  + ",size : " + size());
	
		p = endMarker.next; ///this is line 123 firs error
		for(int i =0;i< idx;i++)
			p=p.next;
		return p;
		
	}
 
	
	public AnyType set(int idx, AnyType newVal) {
		Node<AnyType> p = getNode(idx);
		AnyType oldVal = p.data;
 
		p.data = newVal;
		return oldVal;
	}
 
	
	public AnyType remove(int idx) {
		
		return remove(getNode(idx));
				
	}
 
	
	private AnyType remove(Node<AnyType> p) {
		
		if(theSize ==1) 
		{
			
			//to -do
		
		}
		else {
			
			
		}
		
		theSize--;
		modCount++;
		
		return p.data;
	}
 
	
	public String toString() {
		StringBuilder sb = new StringBuilder("[ ");
 
		for (AnyType x : this)
			sb.append(x + " ");
		sb.append("]");
 
		return new String(sb);
	}
 
	public java.util.Iterator<AnyType> iterator() {
		return new  CircularLinkedListIterator();
	}
 
	private class  CircularLinkedListIterator implements java.util.Iterator<AnyType> {
		
		private Node<AnyType> current ;
		private int count =0;
		private int expectedModCount = modCount;
		private boolean okToRemove = false;
 
		
		public  CircularLinkedListIterator()
		{
			if(endMarker != null) 
			{
			current = endMarker.next;
				
			}
			
			
		}
 
		public boolean hasNext() {
			
			if(current == null)
			{
				return false;
			}
			
			return (count < theSize);
		}
 
		public AnyType next() {
			if (modCount != expectedModCount)
				throw new java.util.ConcurrentModificationException();
			if (!hasNext())
				throw new java.util.NoSuchElementException();
 
			AnyType nextItem = current.data;
			current = current.next;
			okToRemove = true;
			count++;
			return nextItem;
		}
 
		public void remove() {
			if (modCount != expectedModCount)
				throw new java.util.ConcurrentModificationException();
			if (!okToRemove)
				throw new IllegalStateException();
 
			MyCircularLinkedList.this.remove(current.prev);
			expectedModCount++;
			count--;
			
			okToRemove = false;
		}
	}
 
	/**
	 * This is the doubly-linked list node.
	 */
	private static class Node<AnyType> {
		public Node(AnyType d, Node<AnyType> p, Node<AnyType> n) {
			data = d;
			prev = p;
			next = n;
		}
 
		public AnyType data;
		public Node<AnyType> prev;
		public Node<AnyType> next;
	}
}
 
class TestLinkedList {
	
	public static void main(String[] args) {
		MyCircularLinkedList<Integer> lst = new MyCircularLinkedList<>();
 
		for (int i = 0; i < 10; i++)
			lst.add(i); //this line 259 4th error
		
		System.out.println(lst);
		
		for (int i = 20; i < 30; i++)
			lst.add(0, i);
 
		System.out.println(lst);
		
		lst.remove(0);
		lst.remove(lst.size() - 1);
 
		System.out.println(lst);
 
		java.util.Iterator<Integer> itr = lst.iterator();
		while (itr.hasNext()) {
			itr.next();
			itr.remove();
		}
		
		System.out.println(lst);
	}
 
	
}
Posted 13-Feb-13 20:11pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Exception in thread "main" java.lang.NullPointerException
at MyCircularLinkedList.getNode(MyCircularLinkedList.java:123)
at MyCircularLinkedList.add(MyCircularLinkedList.java:67)
at MyCircularLinkedList.add(MyCircularLinkedList.java:59)
at TestLinkedList.main(MyCircularLinkedList.java:259)

The exception you got is presenting you the line in which the Exception is starting.
In this case it's line 59, which delivers the null to line 67 and then to line 123 where it can not be resolved.
Your test is forcing the exception in line 259 - that's just additional info.

Please enable the line numbers, that will make it easier for you to find the line:

Enable line numbers Eclipse[^]

Enable line numbers Netbeans[^]

You can also set a break point on the exception event. Then the code will stop during debugging when the exception is thrown.
Do so by clicking on the link presented in the exception text.

Java Exception Breakpoint[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100