nullpointerexception

Reply

Join Date: Jul 2008
Posts: 18
Reputation: JConnor135 is an unknown quantity at this point 
Solved Threads: 0
JConnor135 JConnor135 is offline Offline
Newbie Poster

nullpointerexception

 
0
  #1
Jul 19th, 2008
Hi, I am trying to write a code that will delete an element from a linked list and then display the updated list, but I keep getting a nullpointerexception, does anyone know where I went wrong?

import java.util.*;

public class IntNode
{
  public int item;
  public IntNode next;
  public IntNode head;
  
  IntNode prev = null;
  IntNode curr = head;
  
  //////Constructor///////  
  public IntNode(int item1, IntNode next1)
{
  item=item1;
  next=next1;
  }
  
  public IntNode delete_nth(int n)
  {
  while(curr.item != n && n > curr.item)
  {
   if(curr.next==null)
     return null;
   else
    {
      prev = curr; //go to next link
      curr = curr.next;
    }
  }
  if(curr == head)
    head = head.next;
  else
    prev.next = curr.next;
  return curr;
    }
  
public static void main(String [] args)
{
  int n;
     
     //Declare a list
     IntNode list1 = new IntNode(1, new IntNode(2, new IntNode(3, new IntNode
                                 (4, new IntNode(5, new IntNode(6, new IntNode
                                  (7, new IntNode(8, null))))))));
     
     Scanner sc = new Scanner(System.in);
     
     System.out.println("number of the element to delete: ");
     
     n=sc.nextInt();
     
     list1.delete_nth(n);
     
     System.out.println(list1);
     
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: nullpointerexception

 
0
  #2
Jul 20th, 2008
Beware of stale-data declarations!

Here's the code revised, but I'm not sure if it will do exactly what you want.

  1. import java.util.*;
  2.  
  3. public class IntNode
  4. {
  5. public int item;
  6. public IntNode next;
  7. public IntNode head;
  8.  
  9. IntNode prev = null;
  10. IntNode curr = head;
  11.  
  12. //////Constructor///////
  13. public IntNode(int item1, IntNode next1)
  14. {
  15.  
  16. head=next1;
  17. item=item1;
  18. }
  19.  
  20. public IntNode delete_nth(int n)
  21. {
  22. curr = head; // you probably meant to do this?
  23. while(curr.item != n && n > curr.item)
  24. {
  25. if(curr.next==null)
  26. return null;
  27. else
  28. {
  29. prev = curr; //go to next link
  30. curr = curr.next;
  31. }
  32. }
  33. if(curr == head)
  34. head = head.next;
  35. else
  36. prev.next = curr.next;
  37. return curr;
  38. }
  39.  
  40. public static void main(String [] args)
  41. {
  42. int n;
  43.  
  44. //Declare a list
  45. IntNode list1 = new IntNode(1, new IntNode(2, new IntNode(3, new IntNode
  46. (4, new IntNode(5, new IntNode(6, new IntNode
  47. (7, new IntNode(8, null))))))));
  48.  
  49. Scanner sc = new Scanner(System.in);
  50.  
  51. System.out.println("number of the element to delete: ");
  52.  
  53. n=sc.nextInt();
  54.  
  55. list1.delete_nth(n);
  56.  
  57. System.out.println(list1);
  58. }
  59. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC