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.