0

I'm working on a project where I must write a class SeparateChainingMap that represents a separate chaining hash table and implements the Map interface.Every time that I run my code I am getting a Null Pointer exception caused by my insert method. Can anyone provide guidance on what I am doing wrong?

I've included all of my code so far below. The NullPointerException is being thrown when I call the insert method.

import java.util.LinkedList;
import java.util.*;


public class SeparateChainingMap<K extends Comparable<? super K>, V>   implements Map<K,V> {

public static final int INITIAL_SIZE = 10;
private int currentSize;
private LinkedList<Pair<K,V>>[] table;


@SuppressWarnings("unchecked")
public SeparateChainingMap() {
    table = (LinkedList<Pair<K,V>>[]) new LinkedList[INITIAL_SIZE]; 
}

@SuppressWarnings("unchecked")
public SeparateChainingMap (int size) {
    LinkedList<Pair<K,V>> [] table = new LinkedList[INITIAL_SIZE];
    for (int i=0; i<table.length; i++) {
        table[i] = new LinkedList<Pair<K,V>>();
    }
}

//Introducing the hash function
private int myHash(Pair<K,V> x) {
    int hashValue = x.hashCode();
    //divide the hashValue by mod table length
    hashValue %= table.length;

    if(hashValue<0) {
        hashValue += table.length;
    }
    System.out.println(hashValue);
    return hashValue;
}

//rehash function
@SuppressWarnings("unchecked")
private void rehash() {
    LinkedList<Pair<K,V>>[] oldTable = table;

    //Create new double-sized empty tables
    table = (LinkedList<Pair<K,V>>[]) new LinkedList[2 * table.length];

    for (int j=0; j< table.length; j++) {
        table[j] = new LinkedList<>();
    } 

    //Copy table over
    currentSize = 0;
    for (int i=0; i<oldTable.length; i++) {
        for(int j=0; j<oldTable[i].size(); j++){
            insert(oldTable[i].get(j));

        }

    }       

}

//Find an item in the hash table. returns true if x is found
public boolean contains(Pair<K,V> x) {
    LinkedList<Pair<K,V>> thisList = table[myHash(x)];
    return thisList.contains(x);
}

//Method for inserting a pair into the hash table
public void insert(Pair<K,V> x) {
    LinkedList<Pair<K,V>> thisList = table[myHash(x)];

    if(!thisList.contains(x))
    {
        thisList.add(x);
    }

    if (++currentSize > table.length) {
        rehash();
    }


}

public void remove (Pair<K,V> x) {
    LinkedList<Pair<K,V>> thisList = table[myHash(x)];

    if(thisList.contains(x)) {
        thisList.remove(x);
        currentSize--; 
    }
}

public void makeEmpty()
{
    for(int i = 0; i<table.length; i++) {
        table[i].clear();
    currentSize = 0;
    }
}

//map functions
@SuppressWarnings("unchecked")
public V get (K key) {
    Pair<K,V> pair = new Pair<K,V>(key,null);
    int i = myHash(pair);

    for(int j=0; j<table.length; j++) {

        if(i == j) {
            for(int k=0; k<table[j].size(); k++)
            {
                if(key.equals(table[j].get(k).key)){
                    V value = table[j].get(k).value;
                    return value;
                }
            }


        }

    }


    return null;
}

public void put(K key, V value) {
    Pair<K,V> pair = new Pair<K,V>(key, value);
    insert(pair);

}


public static void main(String args[]) {
@SuppressWarnings("unchecked")

SeparateChainingMap<String,Integer> SCM = new SeparateChainingMap<>();

SCM.put("Alice", 243);

}

landy1027
  • 63
  • 10
  • 2
    Attach a debugger. Break on exception. Play. The problem is always 'the same'; find out what expression is null and work backwards. – user2864740 Apr 03 '15 at 01:50

1 Answers1

1
@SuppressWarnings("unchecked")
public SeparateChainingMap() {
    table = (LinkedList<Pair<K,V>>[]) new LinkedList[INITIAL_SIZE]; 
}

This constructor's table[i] doesn't initiate. It should same as:

@SuppressWarnings("unchecked")
public SeparateChainingMap (int size) {
    LinkedList<Pair<K,V>> [] table = new LinkedList[INITIAL_SIZE];
    for (int i=0; i<table.length; i++) {
        table[i] = new LinkedList<Pair<K,V>>();
    }
}
chengpohi
  • 14,064
  • 1
  • 24
  • 42