why do i get nullpointerexception in java

Joined
Aug 3, 2009
Messages
3
Reaction score
0
this is where my main is:
public class MyStringHashTableDemo{
public static void main(String[] args){


MyStringHashTable arrayko = new MyStringHashTable(5);

arrayko.insert("epal");
arrayko.insert("array");
arrayko.insert("leithold");
arrayko.insert("jenny");
arrayko.insert("ewan");
System.exit(0);
}
}





//this is my class where i get the message about nullpointerexception:

public class MyStringHashTable{

String[] array;
int arraysize;
MyStringHashTable(){
new MyStringHashTable();
}

MyStringHashTable(int size){
arraysize = size;
array = new String[arraysize];
}

void insert(String s){
int elementIndex;
elementIndex = s.hashCode() % arraysize;

while(array[elementIndex] != null){
arraysize = arraysize*2;
elementIndex = s.hashCode() % arraysize;
array = newPositionArray(arraysize);
}
array[elementIndex] = s;
System.out.println("array[" + elementIndex + "]: " + array[elementIndex]);
System.out.println("It has a hash code: " + array[elementIndex].hashCode());
System.out.println();
}

String[] newPositionArray(int arsize){
int temp;
String[] tempArray = new String[arsize];
int i;

for(i=0; i<tempArray.length; i++){
temp = array.hashCode() % arsize;
tempArray[temp] = array;
}
return tempArray;
}

}





//this code is for collison handling in java.. but i keep getting a nullPointerException..

//this is the output:

array[3]: epal
It has a hash code: 3119638

Exception in thread "main" java.lang.NullPointerException
at MyStringHashTable.newPositionArray(MyStringHashTable.java:35)
at MyStringHashTable.insert(MyStringHashTable.java:21)
at MyStringHashTableDemo.main(MyStringHashTableDemo.java:8)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top