gpt4 book ai didi

java - 为什么我会得到这个空指针异常?

转载 作者:行者123 更新时间:2023-11-29 05:15:27 24 4
gpt4 key购买 nike

<分区>

所以我得到了这个空指针异常。我知道它是在哪一行引起的,我只是无法在我的代码中看到是什么导致了它。

import java.util.*;

public class NoDuplicatesQueueWilson<T> implements NoDuplicatesQueueInterfaceWilson<T>
{
private int MAX_QUEUE = 5; // Default array size, small for testing purposes
private T[] items; // The array of the queue.
private int front; // The first entered item of a queue.
private int back; // The last entered item of a queue.
private int count; // A counter.

public NoDuplicatesQueueWilson() // Default constructor
{
T [] items = (T[]) new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
}

// Begin Queue Operations
// isEmpty() checks the array to determine if there is an items in it.
public boolean isEmpty()
{
return count == 0;
}

// isFull() checks to see if the array is full or not.
public boolean isFull()
{
return count == MAX_QUEUE;
}

// enqueue(Object newItem) adds items to the back of the queue if it is not full.
// If it is full, it will double the size of the array of items,
// and re-create the array, adding the item onto the new array.
public void enqueue(T newItem) {
if(!isFull())
{
back = (back+1) % (MAX_QUEUE);
items[back] = newItem;
++count;
}
else
{
MAX_QUEUE = MAX_QUEUE * 2;
System.out.println("The array was full. We've doubled the size.");
T [] items = (T[]) new Object[MAX_QUEUE];
back = (back+1) % (MAX_QUEUE);
items[back] = newItem;
++count;
} // End if
} // End Enqueue

当我在我的驱动程序(带有测试数据)中运行它时,异常发生在给定代码的第 43 行(我的主类包含方法和构造函数),它位于我的入队方法的中间。特别是这一行:

items[back] = newItem;

关于我可能需要做什么或寻找我的错误所在的任何建议?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com