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

Java 为什么我会得到这个空指针异常?,java,Java,所以我得到了这个nullpointerexception。我知道它是在哪一行引起的,我只是在代码中看不清是什么引起的 import java.util.*; public class NoDuplicatesQueueWilson<T> implements NoDuplicatesQueueInterfaceWilson<T> { private int MAX_QUEUE = 5; // Default array size, small for testing p

所以我得到了这个nullpointerexception。我知道它是在哪一行引起的,我只是在代码中看不清是什么引起的

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

关于我可能需要做什么或寻找什么来查看我的错误在哪里,有什么建议吗?

在构造函数中,您不是在初始化
T[]项,而是在创建新的局部变量。应该是

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

编辑:还请检查
其他方法中的
部分代码。

在构造函数中,此行分配给局部变量,而不是实例变量:

T [] items = (T[]) new Object[MAX_QUEUE];
您可以使用
this.items
访问实例变量
items

this.items = (T[]) new Object[MAX_QUEUE];
您也可以只使用
items=…
,因为在这种情况下没有歧义


T[]items=…
enqueue(T newItem)
方法中也存在相同的错误。您能解释一下back=(back+1)%(MAX_QUEUE);?你是怎么想到这个的?同样,同样的错误出现在队列的else部分。
this.items = (T[]) new Object[MAX_QUEUE];