LinkedList remove method

Gives a null pointer exceptio

Page 1 of 1

2 Replies - 4615 Views - Last Post: 31 January 2010 - 01:03 PM Rate Topic: -----

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

LinkedList remove method

Posted 30 January 2010 - 11:11 PM

Hi!! I'm working on the remove and find method for a linked list, however when I try to delete the last element in the list it gives me a null pointer exception and when I try to delete the first element in the list, nothing happens...



  public Node find(Integer number)
  {
	  Node current = head;

	  if(head==null)
	  {
		  System.out.println("List is empty");
		  return null;
	  }
	 
	  if(head.next==null)
	  {
		 if(head.getInt.compareTo(number)==0)
		 {
			 head = null;
		 }
	  }

	   else
	   {
		   while(current.next != null)
		   {
				 if(current.next.getInt.compareTo(number) == 0)
				 {
					 if(current.next.next != null)
					 {
						current.next = current.next.next;
					 }
					 else
						 current.next = null;
					 
				  }
					
					current = current.next;
				}
			  
			  
		  }
		  return head;
	
  }



Any one could give a hint..that would be nice :-D THX

This post has been edited by jinnyishere: 31 January 2010 - 01:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: LinkedList remove method

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: LinkedList remove method

Posted 31 January 2010 - 04:19 AM

Hey jinny, would you be able to post the whole class, as well as the stacktrace you're getting? This would make it easier to see what's going on.
Was This Post Helpful? 0
  • +
  • -

#3 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: LinkedList remove method

Posted 31 January 2010 - 01:03 PM

hey!! thx..here is the sorted linkList class

/*
	This is the sortedLinked List
 */

package cs240linkedlist;

/** *
 * @author JJ
 */
import java.util.*;

public class SortedLinkedList
{
	Node head, tail;
	int size = 0;

	//Finds the appropriatePosition of the node then returns the head
	public Node AppropriatePosition(Integer getNum)
	{
		  Node temp = new Node(head, getNum);
		  Node current;
		  current= head;

		  
		  if(current == null || head.getInt.compareTo(getNum)>0 )
		  {
			  head = temp;
			  return head;
		  }

		  Node start = head;

		  while (start.next != null)
		  {
			 if(start.next.getInt.compareTo(getNum)>0)
				 break;
			 start = start.next;
		  }
		  start.next = new Node(start.next, getNum);

		  return head;
	}

	//Inserting the element into the node
	public void insert()
	{
		Integer ele;
		Scanner key = new Scanner(System.in);

		System.out.print("Enter an integer: ");
		ele = key.nextInt();

		tail = new Node(head, ele);
		AppropriatePosition(ele);
   
	}

   //This is the delete method
   public void delete()
   {
	  Integer n;
	  Scanner key = new Scanner(System.in);

	  System.out.print("Enter a element: ");
	  n = key.nextInt();

	  find(n);
	  
   }


  public Node find(Integer number)
  {
	  Node current = head;

	  if(head==null)
	  {
		  System.out.println("List is empty");
		  return null;
	  }
	 
	  if(head.next==null)
	  {
		 if(head.getInt.compareTo(number)==0)
		 {
			 head = null;
		 }
	  }

	   else
	   {
		   while(current.next != null)
		   {
				 if(current.next.getInt.compareTo(number) == 0)
				 {
					 if(current.next.next != null)
					 {
						current.next = current.next.next;
					 }
					 else
						 current.next = null;
					 
				  }
					
					current = current.next;
				}
			  
			  
		  }
		  return head;
	
  }


  //check wheater the list is empty or no
  public void clear()
  {
	  head = null;
	  System.out.println("The list is empty now");
  }

	//Printing the smallest element in the List
	public void smallest()
	{
		if(head == null)
		{
			System.out.println("There are no element in the list yet");
			
		}
		else
		{
			System.out.println("Smallest is: " + head.getInt + "\n");

		   

		}
	}

	public void largest()
	{
		if(head==null)
		{
			System.out.println("There are no element in the list yet");
			
		}

		else
		{
			if(head==head.next)
			{
				System.out.print("The largest is " + head.getInt + "\n");
			   
			}
			else
			{
				while(head.next != null)
				{
					head = head.next;
				}
				System.out.println("The largest is " + head.getInt + "\n");
			   
			}

		}

		
	}

	//Prints out the list
	public void display()
	{
		Node temp = head;

		while(temp != null)
		{
			System.out.println(temp.getInt);
			temp = temp.next;
		}
	   System.out.println(" ");
	}
}



this is my node class

/*
]   This is the node class
 */

package cs240linkedlist;

/**
 *
 * @author JJ
 */
public class Node
{
	Node next;
	Integer getInt;
	int index = 0;

	//This is the default constructor
	public Node()
	{
		next = null;

	}

	//A two parameter constructor
	public Node(Node n, Integer getEle)
	{
		next = n;
		getInt = getEle;
	}

	//Return the next Node
	public Node getNext()
	{
		return  next;
	}

	public void setNext(Node n)
	{
		next = n;
	}

}




And this is the error I am getting

run:
1) Insert   2)  Delete   3)  Clear   4)  Smallest  5)  Largest   6)  Exit 

Enter your selection:1
Enter an integer: 2
 
2
 
1) Insert   2)  Delete   3)  Clear   4)  Smallest  5)  Largest   6)  Exit 

Enter your selection:1
Enter an integer: 4
 
2
4
 
1) Insert   2)  Delete   3)  Clear   4)  Smallest  5)  Largest   6)  Exit 

Enter your selection:2
Enter a element: 4
Exception in thread "main" java.lang.NullPointerException
		at cs240linkedlist.SortedLinkedList.find(SortedLinkedList.java:92)
		at cs240linkedlist.SortedLinkedList.delete(SortedLinkedList.java:67)
		at cs240linkedlist.IntDriver.main(IntDriver.java:43)
Java Result: 1


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1