Why when i run this hashtable, it said nullpointerexception of insert method at
ID: 3843959 • Letter: W
Question
Why when i run this hashtable, it said nullpointerexception of insert method at while loop"while(T[k]!= null){" ,cannot figure it out.Please help!
public class HashTable{
//The size of the hash table.
int TableSize;
//The current number of elements in the hash table.
int NumElements;
//The variable T represents the array used for the table.
// DECLARE YOUR TABLE DATA STRUCTURE HERE
String[] T;
public HashTable(){
NumElements = 0;
TableSize = 997;
String[] T=new String[TableSize];
// INITIALZE YOUR TABLE DATA STRUCTURE HERE
}
/* hash(s) = ((3^0)*s[0] + (3^1)*s[1] + (3^2)*s[2] + ... + (3^(k-1))*s[k-1]) mod TableSize
(where s is a string, k is the length of s and s[i] is the i^th character of s)
Return the hash code for the provided string.
The returned value is in the range [0,TableSize-1].
NOTE: do NOT use Java's built in pow function to compute powers of 3. To avoid integer
overflows, write a method that iteratively computes powers and uses modular arithmetic
at each step.
*/
public int hash(String s){
int h = 0;
for(int i=0; i<s.length(); i++){
h += s.charAt(i)*pow(i);
}
return h%TableSize;
}
public int pow(int p){
int ans=3;
if(p==0){
ans=1;
return ans;
}
if(p==1){
return ans;
}
for(int i=2;i<=p;i++){
ans*=ans;
}
return ans;
}
/* insert(s)
Insert the value s into the hash table and return the index at
which it was stored.
*/
public int insert(String s){
NumElements++;
if((double)NumElements/TableSize>=0.75){
TableSize=TableSize*2;
resize(TableSize);
}
int i = hash(s);
int j = 0;
int k = i;
while(T[k]!= null){
j++;
k=(i+(j*j))%TableSize;
}
T[k]=s;
return k;
}
/* find(s)
Search for the string s in the hash table. If s is found, return
the index at which it was found. If s is not found, return -1.
*/
public int find(String s){
int i = hash(s);
int j = 0;
int k = i;
while (true){
String element = T[k];
if (element == null)
return -1;
if (s.equals(element))
return k;
j++;
k=(i+(j*j))%TableSize;
}
}
/* remove(s)
Remove the value s from the hash table if it is present. If s was removed,
return the index at which it was removed from. If s was not removed, return -1.
*/
public int remove(String s){
/*NumElements--;
if((double)NumElements/TableSize<=0.25){
resize(TableSize/2); */
int i = hash(s);
int j = 0;
int k = i;
while (true){
String element = T[k];
if (element == null)
return -1;
if (s.equals(element)){
T[k]=null;
NumElements--;
if((double)NumElements/TableSize<=0.25)
TableSize=TableSize/2;
resize(TableSize);
}
j++;
k=(i+(j*j))%TableSize;
}
}
/* resize()
Resize the hash table to be a prime within the load factor requirements.
*/
public void resize(int newSize){
while(!isPrime(newSize)){
newSize++;
}
String[] T2= new String[newSize];
T=T2;
}
public boolean isPrime(int num){
if(num>2){
for(int i=2;i<num;i++){
if(num%i==0){
return false;
}
return true;
}
}
return false;
}
Explanation / Answer
public class HashTable{
//The size of the hash table.
int TableSize;
//The current number of elements in the hash table.
int NumElements;
//The variable T represents the array used for the table.
// DECLARE YOUR TABLE DATA STRUCTURE HERE
String[] T;
public HashTable(){
NumElements = 0;
TableSize = 997;
T = new String[TableSize];
// INITIALZE YOUR TABLE DATA STRUCTURE HERE
}
/* hash(s) = ((3^0)*s[0] + (3^1)*s[1] + (3^2)*s[2] + ... + (3^(k-1))*s[k-1]) mod TableSize
(where s is a string, k is the length of s and s[i] is the i^th character of s)
Return the hash code for the provided string.
The returned value is in the range [0,TableSize-1].
NOTE: do NOT use Java's built in pow function to compute powers of 3. To avoid integer
overflows, write a method that iteratively computes powers and uses modular arithmetic
at each step.
*/
public int hash(String s){
int h = 0;
for(int i=0; i<s.length(); i++){
h += s.charAt(i)*pow(i);
}
return h%TableSize;
}
public int pow(int p){
int ans=3;
if(p==0){
ans=1;
return ans;
}
if(p==1){
return ans;
}
for(int i=2;i<=p;i++){
ans*=ans;
}
return ans;
}
/* insert(s)
Insert the value s into the hash table and return the index at
which it was stored.
*/
public int insert(String s){
NumElements++;
if((double)NumElements/TableSize>=0.75){
TableSize=TableSize*2;
resize(TableSize);
}
int i = hash(s);
int j = 0;
int k = i;
while(T[k]!= null){
j++;
k=(i+(j*j))%TableSize;
}
T[k]=s;
return k;
}
/* find(s)
Search for the string s in the hash table. If s is found, return
the index at which it was found. If s is not found, return -1.
*/
public int find(String s){
int i = hash(s);
int j = 0;
int k = i;
while (true){
String element = T[k];
if (element == null)
return -1;
if (s.equals(element))
return k;
j++;
k=(i+(j*j))%TableSize;
}
}
/* remove(s)
Remove the value s from the hash table if it is present. If s was removed,
return the index at which it was removed from. If s was not removed, return -1.
*/
public int remove(String s){
/*NumElements--;
if((double)NumElements/TableSize<=0.25){
resize(TableSize/2); */
int i = hash(s);
int j = 0;
int k = i;
while (true){
String element = T[k];
if (element == null)
return -1;
if (s.equals(element)){
T[k]=null;
NumElements--;
if((double)NumElements/TableSize<=0.25)
TableSize=TableSize/2;
resize(TableSize);
}
j++;
k=(i+(j*j))%TableSize;
}
}
/* resize()
Resize the hash table to be a prime within the load factor requirements.
*/
public void resize(int newSize){
while(!isPrime(newSize)){
newSize++;
}
String[] T2= new String[newSize];
T=T2;
}
public boolean isPrime(int num){
if(num>2){
for(int i=2;i<num;i++){
if(num%i==0){
return false;
}
return true;
}
}
return false;
}
}
================================
Run This, Null Pointer is resolved.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.