0

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();
    }
}
1
  • I want to thank you guys for your quick responses, I have some more work to do on it, however, I am no longer getting the null pointer exception since I created the instance of it and assigned values. Commented Mar 6, 2015 at 23:35

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

How would I then structure my output( ) method to reference the hashArray and the SLL.outputList( )?
You have one option, since you can't modify your constructor (says so right in your code), which is to create the node itself if it's null, when you add. When hashing to an empty "bucket", if the space where your object is supposed to live is null, you have to create it first using new SLL()... and then use it. If it's not null, then you can use it like you've got in your code. Your output method can operate using a simple enhanced for loop (for (SLL listObj : hashArray) {...} ) to iterate through your lists and print anything within. Be sure to check for null...
I just figured that out last night about 20 min before it was do, and I have never used that type of for loop syntax before, but i got it to work that exact way. Thanks for all the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.