1 Replies - 593 Views - Last Post: 26 March 2009 - 01:07 AM Rate Topic: -----

#1 shahab.burki   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 09

Help Required in Searching a Node

Post icon  Posted 25 March 2009 - 10:46 PM

Hi,

I am implementing a Double Link List in Array. Though I have implemented it successfully but when I search for a specific Node then a Null-Pointer Exception is thrown. I used while, for loops and other techniques to combat this but failed. I am pasting the code here. Just Focus on the searchNode() method and provide me the help regarding it.

1. Data Class

class Data{
	
	private String name;
	
	Data(String name){
		this.name=name;
	}
	
	public void setData(String name){
		this.name=name;
	}
	public String getData(){
		return (name);
	}
}


2. Node Class

class SNode{
	
	private Data data;
	private SNode next;
	private SNode prev;
	
	SNode(){
		
		next=null;
		prev=null;
	}
	SNode(Data data,SNode next,SNode prev){
		
		this.data=data;
		this.next=next;
		this.prev=prev;
	}
	
	public void setData(Data data){
		
		this.data=data;
	}
	
	public void setNext(SNode next){
		
		this.next=next;
	}
	public void setPrev(SNode prev){
		
		this.prev=prev;
	}
	
	public Data getData(){
		
		return (data);
	}
	
	public SNode getNext(){
		
		return (next);
	}
	
	public SNode getPrev(){
		
		return (prev);
	}
}


3. LinkList Class

//import java.io.*;

class StaticLinkList{
	
		private SNode[] staticList;
		private int start;
		private int last;
		private int size;
		
		StaticLinkList(){
			staticList=null;
			start=0;
			last=0;
			size=0;
		}
		
		StaticLinkList(int size){
			
			this.setSize(size);
			staticList=new SNode[this.getSize()];
	
		}
		
		public void setSize(int size){
			
			this.size=size;
		}

		public int getSize(){
			
			return (size);
		}
		
		public void setStart(int start){
			
			this.start=start;
		}
		public void setLast(int last){
			
			this.last=last;
		}
		public int getLast(){
			
			return (last);
		}
		public int getStart(){
			
			return (start);
		}
		
		public void insertNode(SNode newNode, int index){
			
			
				
				if(index==0 && this.staticList[index]==null){
					this.staticList[index]=newNode;
					this.setStart(index);
					this.setLast(index);
				}
				else{
					
					if(this.staticList[index]==null){
						
						this.staticList[index]=newNode;
						this.setLast(index);
						newNode.setPrev(staticList[index-1]);
						newNode.getPrev().setNext(newNode);
					}
				}
			}
			
		/*public SNode deleteNode(Data data){
			if(this.getStart()==this.getLast() && this.staticList[this.getStart()].getData()==data){
				
								
				return (this.staticList[this.getStart()]);
				
				
				
				
			}
			else if (this.staticList[this.getLast()].getData()==data){
				
				Node pre
				
			}
		}*/
		
		public SNode searchNode(Data key){
			
			
			
			
				for(int i=0; i<=this.staticList.length;i++){
					if(this.staticList[i].getData()==key){
						
						return (this.staticList[i]);
						//break;
					}
					else{
						
					}
					return (this.staticList[this.getLast()]);
				}
				
			}
		
		
		public void showList(){
			
			for(int i=0;i<=this.getLast();i++){
				
				System.out.println("The Node is: "+this.staticList[i].getData().getData());
			}
		}
		
		public void showReverseList(){
			
			for(int i=this.getLast();i>=this.getStart();i--){
				
				System.out.println("The Node is: "+this.staticList[i].getData().getData());
			}
		}
		
		public static void main(String args[]){
			
			/*String input;
			int size=0;
			BufferedReader myReader=new BufferedReader(new InputStreamReader(System.in));
			
			System.out.println("Please Enter The Array Size");
			size=myReader.read();
			StaticLinkList myList=new StaticLinkList(size);
			System.out.println("Please Enter The Names");
			int i=0;
			 while ((input = myReader.readLine()).equalsIgnoreCase("stop") == false)*/{
			StaticLinkList myList=new StaticLinkList(4);
				
			try{
			myList.insertNode(new SNode(new Data("Guldan"),null,null),0);
			myList.insertNode(new SNode(new Data("Janan"),null,null),1);
			myList.insertNode(new SNode(new Data("Kamran"),null,null),2);
			myList.insertNode(new SNode(new Data("Bamyan"),null,null),3);
			
			}catch(Exception e){
				System.out.println("Array is full Now:");
			}
			
			SNode tmp=myList.searchNode(new Data("Kamran"));
			
			//System.out.println(tmp.getData().getData());
			
			//myList.showList();
			//myList.showReverseList();
			}
		
		}
}		





Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Help Required in Searching a Node

#2 Mikeyp926   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Help Required in Searching a Node

Posted 26 March 2009 - 01:07 AM

Hi,

I think the problem is within the condition of the for loop in the searchNode method. I would change it to
for(int i=0; i<=this.staticList.length-1; i++)



Previously you had used "i<=this.staticList.length". This causes a problem on it's final run through the loop. staticList.length is the length of the list, but this is actually one number greater than the last index, because the first index is 0 not 1.
Example: A list with 3 elements, length is 3, but the indexes are 0,1,2. So when the loop runs and tries to access index 3, the null-pointer exception is thrown.


Or at least that is my first suggestion. It's late and I didn't look at how you implemented all the code, but this seems like a pretty common solution to this problem.

Hope this helps!
-Michael

This post has been edited by Mikeyp926: 26 March 2009 - 01:08 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1