This is a program im writing for class.

Here is the exception im getting:

Exception in thread "main" java.lang.NullPointerException
at LinkedList_final.insert(LinkedList_final.java:72)
at LinkedList_final.main(LinkedList_final.java:247)

Basically, my program reads a text file and parse a string. And then, add the string to Linked list.


Thank you for your time and any help you may provide!

import java.util.*;
import java.io.*;


public class LinkedList_final {

	/**
	 * @param args
	 */
	//reference to the head node.
	private Node head;
	private int listCount;
	
	//LinkedList constructor
	public LinkedList_final()
	{
		//this is an empty list, so the reference to the head node
		// is set to a new node with no data.
		head = new Node(null);  // head is null
		listCount=0;
	}
	
	public void add(Object data)
	//appends the specified element to the end of this list.
	{
		Node temp = new Node(data); //data assigns to node temp 
		Node current = head; //current node is head
		
		//starting at the head node, crawl to the end of the list
		while(current.getNext()!=null)
		//find end of list
		{
			current = current.getNext(); //get the next object in the list
		}
		//the last node's "next" reference set to our new node
		//add new node to end of list
		current.setNext(temp); //set the pointer to the next List
		listCount++;// increment the number of elements variable
	}
	
	public void add(Object data, int index)
	//inserts the specified element at the specified position in this list
	{
		Node temp = new Node(data);
		Node current = head;
		//crawl to the requested index or the last element in the list,
		//whichever comes first
		for(int i=1; i<index&&current.getNext()!=null; i++)
		{
			current=current.getNext();
		}
		//set the new node's next-node reference to this node's next-node reference
		temp.setNext(current.getNext());
		//now set this node's next-node reference to the new node
		current.setNext(temp);
		listCount++;
	}
	
	public void insert(Object data)
	//inserts the specified element before the tail
	{
		Node temp = new Node(data);
		Node current = head;
		//crawl to the requested index or the last element in the list,
		//whichever comes first
		for(int i=1; i<listCount&&current.getNext()!=null; i++)
		{
			current=current.getNext();
		}
		//set the new node's next-node reference to this node's next-node reference
		temp.setNext(current.getNext());
		//now set this node's next-node reference to the new node
		current.setNext(temp);
		listCount++;
	}
	public Object get(int index)
	// returns the elements at the specified position in this list.
	{
	      //index must be 1 or higher
		if(index<=0)
			return null;
		
		Node current = head.getNext();
		for(int i=1; i<index; i++)
		{
			if(current.getNext()==null)
				return null;
			current=current.getNext();
		}
		return current.getData();
	}
		
	public boolean remove(Object data)
	//remove the element at the specified element in this list.
	{
		int index = 0;
		Object temp,temp1;
		temp=data;
	      //index must be 1 or higher
		/*if(index<=0)
			return null;*/
		
		Node current = head.getNext();
		
		for(int i=1; i<listCount&&current.getNext()!=null; i++)
		{
			temp1=current.getData();
			if(temp.equals(temp1))
				index=i;
			else
			  current=current.getNext();
			 
		}
		
	   	//if the index is out of range, exit
		if(index<1 || index >size())
			return false;
		
				
		for(int i=1;i<index;i++)
		{
			if(current.getNext()==null)
				return false;
			
			current=current.getNext();
		}
		
		current.setNext(current.getNext().getNext());
		listCount--; // decrement the number of elements variable
		return true;
	}
	
	public void print(Node current)
	{
		Object temp;
		
		if(current==null)
			System.out.println("Empty list");
		else
			for(int i=1; i<listCount&&current.getNext()!=null; i++)
			{
				temp=current.getData();
				System.out.println(temp);
				current=current.getNext();
			}
	}
	public boolean find(Object data)
	// returns the elements at the specified position in this list.
	{
		Object temp,temp1;
		temp=data;
		boolean flag=false;
		Node current = head.getNext();
		
		
		
		for(int i=1; i<listCount&&current.getNext()!=null; i++)
		{
			temp1=current.getData();
			if(temp.equals(temp1))
				flag=true;
			else
		    	current=current.getNext();
		}
		
		return flag;
	}
	
	public int size()
	// returns the number of elements in this list
	{
		return listCount;
	}
	
	public void delete()
	{
		head=null;
		listCount=0;
	}
	
	private class Node
	{
		//reference to the next node in the chain,
		//or null if there isn't one/
		Node next;
		//data carried by this node.
		//could be of any type you need.
		Object data;
		
		//Node constructor
		public Node(Object _data)
		{
			next=null;
			data=_data;
		}
		
		//another Node constructor if we want to
		//specify the node to point to.
		public Node(Object _data, Node _next)
		{
			next=_next;
			data=_data;
		}
		
		public Object getData()
		{
			return data;
		}
		
		public void setData(Object _data)
		{
			data =_data;
		}
		
		public Node getNext()
		{
			return next;
		}
		
		public void setNext(Node _next)
		{
			next = _next;
		}
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
        Scanner in=null;
		BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
		LinkedList_final list=new LinkedList_final();
		String str;
		char temp;
		boolean tf;
		
		try{
			in = new Scanner (new BufferedReader(new FileReader("input.txt")));
			
			while(in.hasNext()){
				str=in.nextLine();
				temp= str.charAt(0);
				temp= Character.toLowerCase(temp);
		        switch(temp)
				{
				case 'a':
					list.add(str.substring(2,(str.length()-1)));
				case 'i':
					list.insert(str.substring(2,(str.length()-1)));
				case 'r':
					list.remove(str.substring(2,(str.length()-1)));
				case 'l':
					list.size();
				case 'f':
				{
					tf= list.find(str.substring(2,(str.length()-1)));
					
					if(tf==true)
						System.out.println(str.substring(2,(str.length()-1))+"found.");
					else
						System.out.println(str.substring(2,(str.length()-1))+"not found.");
						
				}
				case 'p':
				{
					for(int i=1; i < list.size();i++)
					{
						str=list.get(i).toString();
					    out.write(str);
					    out.newLine();
					}
				}
				case 'd':
					list.delete();
				case 's':
				{
					
					for (int i = 1; i < list.size() - 1; i++ )
					{
				            int minIndex = i;		// index of min value
				            String temp1 = list.get(i).toString();	// min value
				            temp=temp1.charAt(0);
				            char temp2;
				            String min;
				            
				            // Find the minimum value in the unsorted part of the array.
				            for ( int j = i + 1; j < list.size(); j++ )
				            {
				            	temp2=list.get(j).toString().charAt(0);
				            	
						     // If this element is less than temp it becomes the min.
				             if ( temp2 < temp )
						     {
							  minIndex = j;
				              min = list.get(j).toString();
						      }
				             
				 
				            // Swap the first item in the unsorted part of the array
					        // with the minimum item (even if the first item is the min).
				            String t = list.get(minIndex).toString();
				            list.add(list.get(i),minIndex);
				            list.add(t,i);
					       }
					 }
					
				}
				}
			}
		}finally{
			if(in !=null){
				in.close();
				out.close();
				
			}
			
		}
        
	}

}
for(int i=1; i<listCount&&current.getNext()!=null; i++)
{
current=current.getNext();
}

temp.setNext(current.getNext());

Your for loop exits once current.getNext() == null. So it is no wonder that when you then reference current.getNext() a few lines of code later, you get a NullPointerException.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.