Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help please

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hey, i have the following code, it makes a map, but i keep getting a null pointer exception in my remove method. Anyone help me??? Pls
import java.lang.Math;
class Node{
Object key;
Object data;
Node next;
public Node (Object k, Object d, Node n){
key = k;
data = d;
next = n;

}//end of constructor
}// end of node class

public class Map{
public Map (int cap){
capacity = cap;
map = new Node[capacity];

int size=0;
int arraySize=0;
int placement = 0;
}//initialiser constructor

public Map () {
capacity = 11;
map = new Node[capacity];
int placement = 0;
int size = 0;
int arraySize =0;
}

private static int arraySize;
private static int capacity;
private static Node map[];
private static Node head;
private static Node oldHead;
private static int size;
private static int placement;

public static Object[] keys (){
System.out.println("KEYS METHOD ENTERED"); System.out.println();
Object arrayB[] = new Object[capacity];
int counterB = 0;
for (int counter = 0; counter < capacity ; counter++){
if (map[counter] != null){//list stored here
for (Node p = map[counter]; p != null; p = p.next){
arrayB[counterB] = p.key;

counterB++;
}//end of for
}//end of if
}//end of for

System.out.println("KEYS METHOD EXITED"); System.out.println();
return arrayB;
}//end of method
public static void remove(Object key){
for(int counter = 0; counter< capacity; counter ++){
// controls movement along array
if(map[counter].key!=null){
// 1) Node could be first in list
// 2) Further into the list
// 3) Not exist in this list, or the Map at all
// 1) If the node is first in the list:
if(map[counter].data == key){
// a) There are no following nodes after this one
// b) There is a list following on.
//a
if(map[counter].next == null){
map[counter]=null;
}
//b
if(map[counter].next != null){
map[counter] = map[counter].next;
}
}//end of if
else{//the node is further in
Node prev = map[counter]; System.out.println("Got here fucker");
for(Node q = map[counter]; q!= null; q=q.next){
//established that not first node, so started at next one
if(q.data == key){// must remove
if(q.next ==null){// then this is the last node in the list
prev.next = null; // node is removed by garbage collector
}
if(q.next!=null){//this is in the middle of a list
prev.next = q.next; // node removed
}
}
else{
prev = q; // other increments controlled by for loop
}

}//end of for
}//end of else
}//end of else
}//end of for loop
}

public static Object get (Object key) {
Object returnKey = "";//done to avoid null being printed out
for(int counter = 0; counter < capacity; counter ++){
if(map[counter] != null){ //there is a list here
for(Node scan = map[counter]; scan!= null; scan = scan.next){
if(scan.data == key){
returnKey = scan.data;
}
}//end of for loop for linked list
}//end of != null if
}//end of for loop
return returnKey;
}//end of get method
public static Object put (Object key, Object value){
Object toReturn = null;
/** System.out.println("PUT METHOD ENTERED"); System.out.println();
System.out.println("Current Key is "+key); */
placement = key.hashCode () % capacity;
/** System.out.println("placement " + placement);
System.out.println("Capacity " + capacity); */
if (map[placement] != null){//means we won't have to rehash as not adding to a new slot in array
System.out.println(" != null entered with key "+ key);
Node scanner = map[placement];
Node prev = scanner; //toReturn remains null, as nothing before it
for(scanner=map[placement]; scanner!=null; scanner = scanner.next){
if(scanner.next == null){//time to add
prev.next = new Node(key, value, null);
toReturn = prev.data;//this is where we added on
}
if(scanner.next!= null){//must keep going thorough the list, so move to scanner, done by for loop
//prev = scanner;
prev = scanner;
}
}
size++;
System.out.println(" != null if exited");
System.out.println();
}//end of if 1
if (map[placement] == null){//then we are about to add to a new section in the array, we must make sure we do not
// exceed the load factor
if ((arraySize + 1) < (Math.floor(capacity * 0.75))){//no rehash is needed
map[placement] = new Node (key, value, null);
arraySize++;
System.out.println(arraySize);
size++;
System.out.println(" Came to no rehash if in put method");
}//end of size 1 if
else{

Object tempKey = key; //don't want these to change during rehash
Object tempVal = value;
Map.rehash();
Map.put (tempKey, tempVal);
arraySize++;
System.out.println(" Came to rehash if");
}//end of if 2

}//end of if 2

return toReturn;
}//end of method
public static int size (){
return size;
}
public static void rehash (){
System.out.println("REHASH METHOD ENTERED"); System.out.println();
Object toBeHashed[] = Map.keys ();

size = 0; // no more objects in the array
int oldCapacity = capacity;
capacity = capacity * 2; arraySize =0;
Node temp[] = new Node[capacity];//because we cannot change the size of the old array

map =temp;
for (int j = 0; j < oldCapacity; j++)
{
if(toBeHashed[j]!=null){
Map.put (toBeHashed[j], toBeHashed[j]);
}
}
System.out.println("REHASH METHOD EXITED");
System.out.println();
}// end of rehash method
} // end of class

Tester Class:
public class A4Test {
public static void main( String a[] ) {
String s[]={"ape","bat","cat","dog","fox","gnu","hog","pig"};
Map m = new Map(3); // init capacity 3
for( int i = 0; i < s.length; i++ ){
m.put( s[i], s[i] );
}

Object b[] = m.keys();
m.remove("ape");
for( int i = 0; i < b.length; i++ )
System.out.print( ((String)m.get( b[i] )) + " ");
System.out.println(Map.size());
System.out.println();
}
}
Th
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
Welcome to JavaRanch!
Please don't post the same message to multiple forums. I'm closing this thread; followups to the copy in Java in General(Intermediate.)
 
    Bookmark Topic Watch Topic
  • New Topic