Both of my getAt and removeAt functions are not working when i use them for my Hash Table implementation.
[Algo.Java] I'm getting a null pointer exception when I'm using the get function while implementing the Hash Table
Are you checking for the existence of the element in the table before quering for its value ?
Yes I am, here are the implementation of both the methods
Remove function from the Hash Table
public V remove(K key) throws Exception{
int bestIndex = hashFunction(key);
LinkedList<HTPair> bucket = this.bucketArray[bestIndex];
HTPair removePair = new HTPair(key,null);
if(bucket == null) {
return null;
}
else {
int findAt = bucket.find(removePair);
if(findAt == -1) {
return null;
}
else {
HTPair pair = bucket.removeAt(findAt);
this.size--;
return pair.value;
}
}
}`
RemoveAt function from the linked list.
` public T removeAt(int index) throws Exception{
if(this.size == 0) {
throw new Exception("Linked List is empty.");
}
if(index<0 || index>=this.size) {
throw new Exception("Index out of bounds.");
}
if(index == 0) {
return removeFirst();
}
else if(index == this.size - 1) {
return removeLast();
}
else {
Node nMinus1 = getNodeAt(index-1);
Node n = nMinus1.next;
Node nPlus1 = n.next;
nMinus1.next = nPlus1;
this.size--;
return n.data;
}
}`
what error is it throwing any kind of exceptions or getting wrong answers with no exceptions thrown ?
When I call for the get function of my hashtable I’m getting a null pointer exception.
Alright, just post your entire code
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
package HashMaps;
public class HashTable<K, V>{
private class HTPair{
K key;
V value;
HTPair(K key, V value){
this.key = key;
this.value = value;
}
public boolean equals(Object other) {
HTPair otherPair = (HTPair) other;
return this.key.equals(otherPair.key);
}
public String toString() {
return "{ " + this.key + " - " + this.value + " }";
}
}
public static final int DEFAULT_CAPACITY = 10;
private LinkedList<HTPair>[] bucketArray;
private int size;
public HashTable() {
this(DEFAULT_CAPACITY);
}
public HashTable(int capacity) {
LinkedList<HTPair>[] list = (LinkedList<HTPair>[]) new LinkedList[capacity];
this.bucketArray = list;
this.size = 0;
}
public void put(K key, V value) throws Exception {
int bestIndex = hashFunction(key);
LinkedList<HTPair> bucket = this.bucketArray[bestIndex];
HTPair addPair = new HTPair(key, value);
if(bucket == null) {
bucket = new LinkedList<>();
bucket.addLast(addPair);
this.bucketArray[bestIndex] = bucket;
this.size++;
}
else {
int findAt = bucket.find(addPair);
if(findAt == -1) {
bucket.addLast(addPair);
this.size++;
}
else {
HTPair pair = bucket.getAt(findAt);
pair.value = value;
}
}
double lamda = (this.size*1.0)/this.bucketArray.length;
if(lamda > 2) {
this.rehash();
}
}
private void rehash() throws Exception{
LinkedList<HTPair>[] oldBucket = this.bucketArray;
this.bucketArray = (LinkedList<HTPair>[]) new LinkedList[2*oldBucket.length];
this.size = 0;
for(LinkedList<HTPair> ob:oldBucket) {
while(ob!=null && !ob.isEmpty()) {
HTPair pair = ob.removeFirst();
this.put(pair.key, pair.value);
}
}
}
public V get(K key) throws Exception{
int bestIndex = hashFunction(key);
LinkedList<HTPair> bucket = this.bucketArray[bestIndex];
HTPair findPair = new HTPair(key, null);
if(bucket == null) {
return null;
}
else {
int findAt = bucket.find(findPair);
if(findAt == -1) {
return null;
}
else {
HTPair pair = bucket.getAt(findAt);
return pair.value;
}
}
}
public V remove(K key) throws Exception{
int bestIndex = hashFunction(key);
LinkedList<HTPair> bucket = this.bucketArray[bestIndex];
HTPair removePair = new HTPair(key,null);
if(bucket == null) {
return null;
}
else {
int findAt = bucket.find(removePair);
if(findAt == -1) {
return null;
}
else {
HTPair pair = bucket.removeAt(findAt);
this.size--;
return pair.value;
}
}
}
private int hashFunction(K key) {
int hashCode = key.hashCode();
hashCode = Math.abs(hashCode);
int bestIndex = hashCode % this.bucketArray.length;
return bestIndex;
}
public void display() throws Exception{
for(LinkedList<HTPair> bucket:this.bucketArray) {
if(bucket != null && !bucket.isEmpty()) {
bucket.display();
}
else {
continue;
}
System.out.println();
}
}
}