Nucleophile
Nucleophile

Reputation: 29

Nullpointer exception in hash table array search

I am creating a hash table in java, and need to do some analysis. One of my analysis tasks is to compare the number of slots in a hash table that have more than one value attached (the percentage of hash clashes, in essence).

Given a prime number (arbitrary decision), I fill the hash table with random numbers. The amount of randoms I generate is equal to 0.8*primeNumber. I am using separate chaining to handle hash clashes. Although the array in the hash table stores nodes, each node can point to another node (so it is really an array of linked lists).

My problem is when I am trying to traverse the bucket (our array holding nodes) to see if each position in the array (each node) has a next element (by next element I mean a next node, which means that a hash clash occured at that position). I am encountering a null pointer exception consistently, and it seems to be when I am checking if a node is pointing to a next ndoe.

Upvotes: 2

Views: 146

Answers (1)

4J41
4J41

Reputation: 5095

Change

if (hPrime.getBucket()[i].hasNext()) {

to

if (hPrime.getBucket()[i] != null && hPrime.getBucket()[i].hasNext()) {

Upvotes: 1

Related Questions