任何人都可以告诉我为什么以下代码生成空指针异常?

时间:2015-04-03 01:47:59

标签: java exception nullpointerexception

我正在开发一个项目,我必须编写一个类SeparateChainingMap,它表示一个单独的链接哈希表并实现Map接口。每次运行我的代码时,我都会得到一个由insert方法引起的空指针异常。任何人都可以提供有关我做错的指导吗?

到目前为止,我已经包含了所有代码。当我调用insert方法时,抛出NullPointerException。

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);

}

1 个答案:

答案 0 :(得分:1)

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

此构造函数的表[i]不会启动。它应该如下:

@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>>();
    }
}