Can someone please help me find out why I am getting a nullpointerexception for this code. Ive been racking my brain and cant find it for the life of me. The program is supposed to delete an element from the linked list that is built in the main method and then display the updated list.

import java.util.*;
public class IntNode
{
 public int item; //element in list
 public IntNode next; //next node in list
 public IntNode head; //reference to first node in list

  
   //////Constructor///////  
 public IntNode(int item1, IntNode next1)
{
  item=item1;
  next=next1;
  }
  
public void delete_nth(int n)
{
  IntNode prev;
  IntNode curr = head;
    
  while(curr != null && n > curr.item)
  {
   if(curr.next==null)
     System.out.println("error: out of bounds");
   else
    {
      prev = curr; //go to next link
      curr = curr.next;
    }
  }
  if(head.item==n)
    head = curr.next;
  else
    prev = curr.next;
}
  
public static void main(String [] args)
{
  int n;
     
     //Declare a list
     IntNode list = new IntNode(0, 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();
     
     list.delete_nth(n);
     
     System.out.println("result" + list);
     }
}

You don't need to keep posting different topics about this.

Since I'm not very helpful, I will for the last time point out the error then quit replying.

import java.util.*;
public class IntNode
{
 public int item; //element in list
 public IntNode next; //next node in list
 public IntNode head; //reference to first node in list

  
   //////Constructor///////  
 public IntNode(int item1, IntNode next1)
{
  item=item1;
  next=next1;
  }
  
public void delete_nth(int n)
{
  IntNode prev;
  IntNode curr = head; // curr points to head, head isn't pointing to anything so it's null
                                      //curr.item you're still trying to evaluate an object in a null reference
  while(curr != null && n > curr.item)

sorry, I just cant figure this out, I know that head isnt pointing to anything, I just dont know how to get it to point to the first node.

Ok this is my last attempt to help you, hopefully this is self-explanatory.

import java.util.*;
public class IntNode3
{
	 public int item; //element in list
	 public IntNode3 next; //next node in list
	 public static IntNode3 head; //reference to first node in list


   //////Constructor///////
	 public IntNode3(int item1, IntNode3 next1)
	 {
		head = next1; // assigning absolute first IntNode3 to static reference head (this happens once and only once)
		item=item1; // placing item argument into item
		next=next1; // assigning the next node to be that of the argument. (when this object is initially created, the first
		            // Node value inserted will act as the head due to static reference
	 }

	public void delete_nth(int n)
	{
	  IntNode3 prev = null;
	  IntNode3 curr = head;

	  while(curr.next != null && n > curr.item) // curr.next shouldn't point to null now, sinece curr points to head which points to first node added
	  {                                        // not sure why you want to specify a number manually, it can be done automatically
		if(curr.next==null)
			System.out.println("error: out of bounds");
		else
		{
			prev = curr; //go to next link
			curr = curr.next;
		}
	  }
	  if(curr.item==n && prev != null){ // if the object pointed by the current is equal to n, we found the value we want to delete
	    prev.next = curr.next;
	  }
	  else
	    head = curr.next; // if the value was zero (the first) then obviously curr.next = head.next and prev is null, so set head to head.next
	}

	@Override
	public String toString(){
		String val = "";
		IntNode3 temp = head;
		while(temp != null){
			val += temp.item;
			temp = temp.next;
		}
		return val;
	}

	public static void main(String [] args)
	{
	  int n;

	     //Declare a list
	     IntNode3 list = new IntNode3(0, new IntNode3(1, new IntNode3(2, new IntNode3(3, new IntNode3
					 (4, new IntNode3(5, new IntNode3(6, new IntNode3
					  (7, new IntNode3(8, null)))))))));




	     Scanner sc = new Scanner(System.in);

	     System.out.println("number of the element to delete: ");

	     n=sc.nextInt();

	     list.delete_nth(n);

	     System.out.println("result" + list);
	}
}
This article has been dead for over six months. Start a new discussion instead.