Results 1 to 2 of 2
  1. #1
    kevingregg is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default trying to build a hash table, getting null pointer exception on search

    Hello everyone I am pretty new to java and I am trying to build a hash table out of linked lists. I have 4 files so far:

    HashTable.java
    Java Code:
    public class HashTable {
    	boolean isEmpty;
    	LinkedListForHash HashList;
    	
    	public HashTable()
    	{
    		isEmpty = true;
    		HashList = new LinkedListForHash();
    	}
    	
    	public LinkedList insertIndex (String indexValue){
    		
    		
    		//searches to see if indexValue is in the hashlist. if it is, it returns the linked list corresponding to indexValue
    		//if it is not, it creates an index of indexValue in the hashlist and returns the newly created empty linked list of that index
    		//boolean alreadyInserted = false;
    		LinkedListForHash TempList = new LinkedListForHash();
    		TempList = HashList;
    		
    		
    			while (!TempList.amEmpty)//first try to return the list with the correct index
    			{
    				if (TempList.info.compareTo(indexValue) == 0)
    					{
    						//alreadyInserted = true;
    						//isEmpty = false;
    						return TempList.infolList;	
    					}
    				else TempList = TempList.nextList;
    			}
    	
    		
    		//if (!alreadyInserted)
    			{
    				TempList.insert(indexValue);
    				isEmpty = false;
    				return TempList.infolList;
    			}
    		
    		//else return null;
    		
    		}
    
    	public void insertInfo (String inInfo)
    		{
    		//places inInfo in the correct list. creates the list if needed
    		String index = calcIndex(inInfo);
    		insertIndex(index).insert(inInfo);
    		}
    	
    	public String calcIndex (String inInfo)
    		{
    		String hashedString = "";
    		if ( inInfo.length() < 4 )
    		{
    			hashedString = inInfo;
    			while (hashedString.length() < 4)
    			{
    				hashedString = hashedString + " ";
    			}			
    		}
    		else if ( inInfo.length() < 8 )
    		{
    			hashedString = inInfo.substring(0, 4);
    		}
    		else 
    		{
    			for (int i = 1; hashedString.length() < 4; i = i + 2)
    			{
    				hashedString = hashedString + inInfo.charAt(i);
    			}
    		}
    		return hashedString;
    		}
    
    	public LinkedList findInfo (String searchInfo)
    		{
    		//reference to first of the lists with searchinfo in it
    		String index = calcIndex(searchInfo);
    		LinkedListForHash TempList = new LinkedListForHash();
    		TempList = HashList;
    		do	
    			if (TempList.info.compareTo(index) == 0)
    				{
    					if (TempList.infolList.search(searchInfo) == searchInfo)
    					return TempList.infolList;
    					else return null;
    				}
    			else TempList = TempList.nextList;
    			while (!TempList.isEmpty());
    		return null;
    		}
    	
    	public boolean isEmpty()
    		{
    		if (isEmpty) return true;
    		else return false;
    		}
    	
    	public boolean isIndexListEmpty(){
    	
    	LinkedListForHash TempList = new LinkedListForHash();
    	TempList = HashList;
    	
    		do {
    		if (TempList.infolList != null){
    			return true;
    		}
    		TempList = TempList.nextList;}
    		while (TempList.nextList != null);
    			
    		return false;
    		}
    	
    	public void deleteInfo (String outInfo)
    		{
    		//calc hash of outinfo and delete outinfo from correct list
    		if (findInfo(outInfo) != null)
    		{
    		String index = calcIndex(outInfo);
    		insertIndex(index).delete(outInfo);
    		}
    		}
    }
    LinkedList.java
    Java Code:
    
    
    
    
    public class LinkedList {
    	*
    * * String info;
    * * LinkedList nextList;
    * * boolean amEmpty;
    * **
    *
    * **
    * * public LinkedList() {
    * * * * info = "";
    * * * * nextList = null;
    * * * * amEmpty = true;
    * * }
    * * public boolean isEmpty() {
    * * * * return (amEmpty);
    * * }
    * * public void insert(String inInfo) {
    * * * * if (this.amEmpty) {
    * * * * * * LinkedList tempLList = new LinkedList();
    * * * * * * // insert info here and add a new empty at end
    * * * * * * info = inInfo;
    * * * * * * nextList = tempLList;
    * * * * * * amEmpty = false;
    * * * * }
    * * * * else {
    * * * * * **
    * * * * * * this.nextList.insert(this.info);
    * * * * * * this.info = inInfo;
    * * * * * **
    * * * * }
    * * }
    * **
    * * //new function: search. returns true if searchInfo is in the list, null otherwise
    * * public String search(String searchInfo)
    * * {
    * * 	 if (this.info.compareTo(searchInfo) != 0)
    * * 	 {
    * * 		 if (this.nextList != null)*
    * * 			 {
    * * 			 	this.nextList.search(searchInfo);
    * * 			 	return "";
    * * 			 }
    * * 		 else return "";
    * * 	 }
    * * 	 else return searchInfo;
    * * }
    * **
    * * public void delete(String outInfo) {
    * * * * if (this.amEmpty)
    * * * * * * return;
    **
    * * * * // check if this is the info to delete
    * * * * if (this.info.compareTo(outInfo) != 0) {
    // * * * * * * * *System.out.println("Didn't match so looking further down list");
    * * * * * * * * this.nextList.delete(outInfo);
    * * * * * * * * return;
    * * * * }*
    * * * * // found it so delete it
    * * * * if (this.nextList.isEmpty()) {
    * * * * * * * * this.nextList = null;
    * * * * * * * * this.info = "";
    * * * * * * * * amEmpty = true;
    // * * * * * * * *System.out.println("Deleting from last element");
    * * * * }
    * * * * else {
    * * * * * * * * this.info = this.nextList.info; // copy next info to current
    * * * * * * * * this.nextList.delete(this.info);
    // * * * * * * * *System.out.println("Shifted info forward and deleting down rest of list");
    * * * * * * }
    * * * * }
    * * public String traverseList() {
    * * * * // add current content to list returned by the rest of the list
    * * * * if (this.isEmpty())
    * * * * * * return "";
    * * * * if (this.nextList.isEmpty())
    * * * * * * return this.info;
    * * * * return this.info+", "+this.nextList.traverseList();
    * * }
    }
    LinkedListForHash.java
    Java Code:
    public class LinkedListForHash extends LinkedList {
    	
    LinkedList infolList;
    LinkedListForHash nextList;
    
    public LinkedListForHash(){
            info = "";
            nextList = null;
            amEmpty = true;
            infolList = new LinkedList();
        }
    public boolean isEmpty() {
        return (amEmpty);
    }
    public String traverseList() {
        // add current content to list returned by the rest of the list
        if (this.isEmpty())
            return "";
        if (this.nextList.isEmpty())
            return this.info;
        return this.info+", "+this.nextList.traverseList();
    }
    public void insert(String inInfo) {
        if (this.amEmpty) {
            LinkedListForHash tempLList = new LinkedListForHash();
            // insert info here and add a new empty at end
            info = inInfo;
            nextList = tempLList;
            amEmpty = false;
        }
        else {
            
            this.nextList.insert(this.info);
            this.info = inInfo;
            
        }
    }
    }
    and finally, tester.java
    Java Code:
    public class tester {
    
    
    	public static void main(String[] args) {
    		
    		LinkedList list = new LinkedList();
    		list.insert("hello1");
    		list.insert("uber test 1");
    		list.delete("hello1");
    		System.out.println(list.traverseList());
    		LinkedListForHash list2 =  new LinkedListForHash();
    		list2.insert("uber test 2");
    		System.out.println(list2.traverseList());
    
    		
    		HashTable table = new HashTable();
    		table.insertInfo("hell");
    		table.insertInfo("hello1234");
    		table.insertInfo("hello123");
    		table.insertInfo("hello12");
    		System.out.println("index list:");
    		System.out.println(table.HashList.traverseList());
    		System.out.println("first list with same hash as hello123:");
    		System.out.println(table.findInfo("hello123").traverseList());
    		System.out.println("first list with same hash as hello12:");
    		System.out.println(table.findInfo("hello12").traverseList());
    		System.out.println("first list with same hash as hell:");
    		System.out.println(table.findInfo("hell").traverseList());
    		System.out.println(table.findInfo("hello1234").traverseList());
    		
    		return;
    	}
    
    }


    The console output I get on running the program is

    uber test 1
    uber test 2
    index list:
    hell, el13
    first list with same hash as hello123:
    hello123, hello1234
    first list with same hash as hello12:
    hello12, hell
    first list with same hash as hell:
    Exception in thread "main" java.lang.NullPointerException
    at tester.main(tester.java:29)

    As far as I can tell, the problem is with my HashTable.findInfo() function which calls LinkedList.search(). The code executes fine if it doesn't have to go into the if statement (as far as I can tell) so I think the problem must be with this block in LinkedList.search():

    Java Code:
    if (this.info.compareTo(searchInfo) != 0)
        	 {
        		 if (this.nextList != null) 
        			 {
        			 	this.nextList.search(searchInfo);
        			 	return "";
        			 }
        		 else return "";
        	 }
    I have no idea why it isn't working though, I hope someone can help me out.
    Last edited by kevingregg; 06-19-2013 at 08:29 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: trying to build a hash table, getting null pointer exception on search

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 13
    Last Post: 12-08-2011, 03:21 AM
  2. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 02:41 AM
  3. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 06:04 PM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •