import java.util.*;
class HashingDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please input the size of the hash table: ");
int tableSize = keyboard.nextInt();
LinkedListN[] hashTable = new LinkedListN[tableSize];
// this works
LinkedListN list = new LinkedListN();
list.addToEnd(50);
System.out.println(list);
//
System.out.print("Enter the number of keys to be hashed: ");
int numberOfKeys = keyboard.nextInt();
Random randomGenerator = new Random();
for (int i = 0; i < numberOfKeys; i++) {
int randomNumber = randomGenerator.nextInt(10000) + 1;
int location = randomNumber % tableSize;
hashTable[location].addToEnd(randomNumber);
}
}
}
LinkedListNはカスタムクラスです(コードは以下に添付されています)。これは、配列がジェネリックスをうまく処理できないためです。
しかし、このプログラムを実行するたびに、次のエラーが発生します。
Please input the size of the hash table: 10
LinkedListN@5265a77f
Enter the number of keys to be hashed: 20
Exception in thread "main" java.lang.NullPointerException
at HashingDemo.main(HashingDemo.java:30)
上でコメントしたように、LinkedListNが1つだけあり、それにデータを追加すれば、問題はありません。ここはどうしたの?私はそれを理解しようとしましたが、できません。