| | |
nullpointerexception
![]() |
•
•
Join Date: Jul 2008
Posts: 18
Reputation:
Solved Threads: 0
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);
}
} Beware of stale-data declarations!
Here's the code revised, but I'm not sure if it will do exactly what you want.
Here's the code revised, but I'm not sure if it will do exactly what you want.
java Syntax (Toggle Plain Text)
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) { head=next1; item=item1; } public IntNode delete_nth(int n) { curr = head; // you probably meant to do this? 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); } }
![]() |
Similar Threads
- Word Association Game (Posting Games)
- NullPointerexception ?? (Java)
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Java)
- [B]NullPointerException[/B] (Java)
- Multi class multi form j2me app getting NullPointerException (Java)
- NullPointerException: (Java)
- Need some help with a Java lab... (Java)
Other Threads in the Java Forum
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans objects oriented output phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows





