title: Can anyone tell me why the following code is generating a Null Pointer Exception?
body: <p>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?</p>
  
  <p>I've included all of my code so far below. The NullPointerException is being thrown when I call the insert method. </p>
  
  <pre><code>import java.util.LinkedList;
  import java.util.*;
  
  
  public class SeparateChainingMap&lt;K extends Comparable&lt;? super K&gt;, V&gt;   implements Map&lt;K,V&gt; {
  
  public static final int INITIAL_SIZE = 10;
  private int currentSize;
  private LinkedList&lt;Pair&lt;K,V&gt;&gt;[] table;
  
  
  @SuppressWarnings("unchecked")
  public SeparateChainingMap() {
      table = (LinkedList&lt;Pair&lt;K,V&gt;&gt;[]) new LinkedList[INITIAL_SIZE]; 
  }
  
  @SuppressWarnings("unchecked")
  public SeparateChainingMap (int size) {
      LinkedList&lt;Pair&lt;K,V&gt;&gt; [] table = new LinkedList[INITIAL_SIZE];
      for (int i=0; i&lt;table.length; i++) {
          table[i] = new LinkedList&lt;Pair&lt;K,V&gt;&gt;();
      }
  }
  
  //Introducing the hash function
  private int myHash(Pair&lt;K,V&gt; x) {
      int hashValue = x.hashCode();
      //divide the hashValue by mod table length
      hashValue %= table.length;
  
      if(hashValue&lt;0) {
          hashValue += table.length;
      }
      System.out.println(hashValue);
      return hashValue;
  }
  
  //rehash function
  @SuppressWarnings("unchecked")
  private void rehash() {
      LinkedList&lt;Pair&lt;K,V&gt;&gt;[] oldTable = table;
  
      //Create new double-sized empty tables
      table = (LinkedList&lt;Pair&lt;K,V&gt;&gt;[]) new LinkedList[2 * table.length];
  
      for (int j=0; j&lt; table.length; j++) {
          table[j] = new LinkedList&lt;&gt;();
      } 
  
      //Copy table over
      currentSize = 0;
      for (int i=0; i&lt;oldTable.length; i++) {
          for(int j=0; j&lt;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&lt;K,V&gt; x) {
      LinkedList&lt;Pair&lt;K,V&gt;&gt; thisList = table[myHash(x)];
      return thisList.contains(x);
  }
  
  //Method for inserting a pair into the hash table
  public void insert(Pair&lt;K,V&gt; x) {
      LinkedList&lt;Pair&lt;K,V&gt;&gt; thisList = table[myHash(x)];
  
      if(!thisList.contains(x))
      {
          thisList.add(x);
      }
  
      if (++currentSize &gt; table.length) {
          rehash();
      }
  
  
  }
  
  public void remove (Pair&lt;K,V&gt; x) {
      LinkedList&lt;Pair&lt;K,V&gt;&gt; thisList = table[myHash(x)];
  
      if(thisList.contains(x)) {
          thisList.remove(x);
          currentSize--; 
      }
  }
  
  public void makeEmpty()
  {
      for(int i = 0; i&lt;table.length; i++) {
          table[i].clear();
      currentSize = 0;
      }
  }
  
  //map functions
  @SuppressWarnings("unchecked")
  public V get (K key) {
      Pair&lt;K,V&gt; pair = new Pair&lt;K,V&gt;(key,null);
      int i = myHash(pair);
  
      for(int j=0; j&lt;table.length; j++) {
  
          if(i == j) {
              for(int k=0; k&lt;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&lt;K,V&gt; pair = new Pair&lt;K,V&gt;(key, value);
      insert(pair);
  
  }
  
  
  public static void main(String args[]) {
  @SuppressWarnings("unchecked")
  
  SeparateChainingMap&lt;String,Integer&gt; SCM = new SeparateChainingMap&lt;&gt;();
  
  SCM.put("Alice", 243);
  </code></pre>
  
  <p>}</p>