Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    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
     
     
     
     
     
    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
     
    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
     
    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():

    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

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

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/78517-trying-build-hash-table-getting-null-pointer-exception-search.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help With Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 15th, 2012, 10:41 PM
  2. Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 15th, 2012, 10:33 PM
  3. null pointer exception
    By Ramzi89 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 15th, 2012, 01:57 PM
  4. [SOLVED] Getting a null pointer exception.
    By Ouzi in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 16th, 2012, 11:25 AM
  5. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM