Java 将我的 LinkedList 实例化到我的数组时出现问题。数据结构课程

Java issues instantiating my LinkedList to my array. Data Structures course

我已经在互联网上搜索过了,但找不到我的问题的答案。我目前正在上数据结构课程,这对这个问题很重要,因为我需要从头开始制作所有内容。我目前正在做作业,问题是这样的:

Using a USet, implement a Bag. A Bag is like a USet—it supports the add(x), remove(x), and find(x) methods—but it allows duplicate elements to be stored. The find(x) operation in a Bag returns some element (if any) that is equal to x. In addition, a Bag supports the findAll(x) operation that returns a list of all elements in the Bag that are equal to x.

我几乎完成了所有工作,现在当我尝试测试我的代码时,它立即抛出一个空指针异常。我通过了调试器,虽然我知道它在哪里失败(当试图创建我的数组列表并用链表填充它时)我只是不知道如何修复它。以下是错误状态:

Exception in thread "main" java.lang.NullPointerException 
at Bag.<init>(Bag.java:10)
at Bag.main(Bag.java:198)

因为我什至还没有启动它,我显然不知道它会遇到任何其他错误,但是当这个问题被修复时我会面对这些错误。我感谢任何帮助。

温馨提示:我无法使用预建的java词典,一切都需要从基础开始。

这是我的全部代码:

public class Bag<T> {

        final int ARR_SIZE = 128;
        LinkedList[] theArray;

        public Bag() {

            for (int i = 0; i < ARR_SIZE; i++) {
                theArray[i] = new LinkedList();
            }
        }

        public boolean add(T x) {
            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;

            theArray[hashKey].addFirst(element, hashKey);
            return true;
        }

        public T find(T x) {

            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;

            return theArray[hashKey].findNode(element).getData();
        }

        public T findAll(T x) {
            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;

            System.out.print(theArray[hashKey].findAllElements(element));

            return element;
        }

        public T remove(T x) {
            T element = x;
            int hashKey = element.hashCode() % ARR_SIZE;
            return theArray[hashKey].removeElement(element);
        }

        public int size() {
            return ARR_SIZE;
        }

        public class Node {

            T data;
            int key;
            Node next;
            Node prev;

            public Node(T t, int k, Node p, Node n) {
                data = t;
                key = k;
                prev = p;
                next = n;
            }

            public T getData() {
                return data;
            }

            public void setData(T data) {
                this.data = data;
            }

            public Node getNext() {
                return next;
            }

            public void setNext(Node next) {
                this.next = next;
            }

            public Node getPrev() {
                return prev;
            }

            public void setPrev(Node prev) {
                this.prev = prev;
            }

            public void display() {
                System.out.println(data);
            }
        }

        public class LinkedList {

            Node header;
            Node trailer;
            int size = 0;

            public LinkedList() {
                header = new Node(null, -1, trailer, null);
                trailer = new Node(null, -1, null, null);
                header.setNext(trailer);
            }

            public int size() {
                return size;
            }

            public boolean isEmpty() {
                return size() == 0;
            }

            public void addFirst(T t, int hashKey) {
                Node currentLast = header.getNext();
                Node newest = new Node(t, hashKey, header, currentLast);
                header.setNext(newest);
                currentLast.setPrev(newest);
                size++;
            }

            public T add(T t) {
                Node currentLast = header.getNext();
                Node newest = new Node(t, -1, header, currentLast);
                header.setNext(newest);
                currentLast.setPrev(newest);
                size++;

                return newest.getData();
            }

            public T removeElement(T t) {
                if (isEmpty()) {
                    return null;
                }

                T element = t;
                return removeNode(findNode(element));
            }

            public T removeNode(Node node) {
                if (isEmpty()) {
                    return null;
                }

                Node pred = node.getPrev();
                Node succ = node.getNext();
                pred.setNext(succ);
                succ.setPrev(pred);
                size--;

                return node.getData();
            }

            public LinkedList findAllElements(T t) {
                Node current = header.getNext();
                T element = t;

                if (isEmpty()) {
                    return null;
                }

                LinkedList all = new LinkedList();

                while (current != null) {
                    if (current.getData() == element) {
                        all.addFirst(element, -1);
                    } else {
                        current = current.getNext();
                    }
                }
                return all;
            }

            public Node findNode(T t) {
                Node current = header.getNext();
                T element = t;

                if (isEmpty()) {
                    return null;
                }

                while (current.getNext() != null && current.getData() != element) {
                    current = current.getNext();
                }

                if (current.getNext() == null && current.getData() != element) {
                    System.out.println("Does not exist");
                }

                return current;
            }
        }

        public static void main(String[] args) {

            Bag<Integer> bag = new Bag();
            bag.add(1);
            bag.add(1);
            bag.add(2);
            bag.add(2);
            bag.add(8);
            bag.add(5);
            bag.add(90);
            bag.add(43);
            bag.add(43);
            bag.add(77);
            bag.add(100);
            bag.add(88);
            bag.add(555);
            bag.add(345);
            bag.add(555);
            bag.add(999);

            bag.find(1);
        }
    }

初始化您的 theArray 以避免 NullPointerExceptions

LinkedList[] theArray = new LinkedList[ARR_SIZE];