TonyPall
TonyPall

Reputation: 9

Null Pointer Exception with Singly Linked List to hashtable Java

This is the initial class provided which we cannot modify

public class SLL {
    public class Node {
        private int data;
        private Node next;

        public Node() {
            data = 0;
            next = null;
        }

        public Node(int newData, Node linkValue) {
            data = newData;
            next = linkValue;
        }

        public int getData() {
            return data;
        }

        public Node getLink() {
            return next;
        }
    }// End of Node inner class

    private Node head;

    public SLL() {
        head = null;
    }

    public void addToStart(int itemData) {
        head = new Node(itemData, head);
    }

    public boolean contains(int item) {
        return (find(item) != null);
    }

    /**
     * Finds the first node containing the target item, and returns a reference
     * to that node. If target is not in the list, null is returned.
     */
    public Node find(int target) {
        Node position = head;
        int itemAtPosition;
        while (position != null) {
            itemAtPosition = position.data;
            if (itemAtPosition == target) {
                return position;
            }
            position = position.next;
        }
        return null; // target was not found
    }

    public void outputList() {
        Node position = head;
        while (position != null) {
            System.out.print(position.data + "  ");
            position = position.next;
        }
        System.out.println();
    }
}

And this is the Set class that we are supposed to finish to get the Tester to work and I keep getting a Null Pointer Exception with my add method, however, it is almost exactly as I have seen in other codes including our text book. Any insight would be very much appreciated as my instructor has pre-made powerpoints and doesn't explain anything or offer any advice to students seeking help.

public class Set {
    private SLL[] hashArray; // DO NOT MODIFY THIS LINE
    private int size = 10; // DO NOT MODIFY THIS LINE

    // DO NOT MODIFY THIS METHOD
    public Set() {
        hashArray = new SLL[size];
    }

    // DO NOT MODIFY THIS METHOD
    private int computeHash(int s) {
        return s % size;
    }

    // COMPLETE BELOW

        public void add(int x)
    {
        int hash = computeHash(x);  // Get hash value
        SLL list = hashArray[hash];
                if (!list.contains(x))
        {
            // Only add the target if it's not already
            // on the list.
            list.addToStart(x);/*replaced hashArray[hash] with list*/
        }               
        }

        public void output( )
        {
            System.out.println("I will work on this later");
        }     
}

Finally, the Tester...

public class Tester{

    // Have this method to display your name, instead.
    static void displayName(){
        System.out.println("Program written by Tony.\n");
    }

    // DO NOT MODIFY THE MAIN METHOD
    public static void main(String[] args){
        displayName();
        Set set1 = new Set();
        Set set2 = new Set();

        set1.add(3);
        set1.add(3);
        set1.add(13);
        set1.add(23);
        set1.add(4);
        set1.add(5);

        set2.add(15);
        set2.add(6);
        set2.add(6);

        System.out.println("Contents of set 'set1': ");
        set1.output();
        System.out.println("Contents of set 'set2': ");
        set2.output();
        System.out.println();
    }
}

Upvotes: 0

Views: 173

Answers (2)

Ryan J
Ryan J

Reputation: 8323

This line isn't doing what you think it's doing.

hashArray = new SLL[size];

You need to actually create each SLL that will populate the array once the array itself is created.

Upvotes: 0

Matthew Loring
Matthew Loring

Reputation: 11

I don't want to give the answer directly as this is likely a homework assignment (correct me if I am wrong). Consider the very first time the add method is called on a newly constructed set. What values are in all indices of "hashArray" at this time and what does that mean for the local variable "list" in your add method?

Upvotes: 1

Related Questions