单链接列表队列中的java.lang.NullPointerException

单链接列表队列中的java.lang.NullPointerException,java,nullpointerexception,linked-list,queue,Java,Nullpointerexception,Linked List,Queue,我到处找遍了,教授给我的建议并不像我希望的那样清晰。我试图将某个对象添加到我的队列中,但我一直收到NPE错误。有趣的是,当我有一个或多个队列时,队列工作正常,排队工作正常。只有当我尝试使用我的特定对象类型队列时,它才会出现问题。你们能帮我查一下我的密码吗 我的老师让我确保我已经为队列分配了内存,并且我检查了队列是否为空,但我确信我两者都在做 Queue.java - package queues; import java.util.Iterator; public clas

我到处找遍了,教授给我的建议并不像我希望的那样清晰。我试图将某个对象添加到我的队列中,但我一直收到NPE错误。有趣的是,当我有一个或多个队列时,队列工作正常,排队工作正常。只有当我尝试使用我的特定对象类型队列时,它才会出现问题。你们能帮我查一下我的密码吗

我的老师让我确保我已经为队列分配了内存,并且我检查了队列是否为空,但我确信我两者都在做

    Queue.java -

    package queues;

import java.util.Iterator;

public class Queue<Type> implements Iterable {

   private String name;
   private Node head, tail;
   private int size;

   //basic constructor for the queue
   public Queue (String name){
      System.out.println("Constructing queue");
      this.name = name;
      this.size = 0;
   }

   //front of the list = end of the queue
   public boolean enqueue(Type data){
      System.out.println("Enqueue");
      if(this.isEmpty()){
         Node newNode = new Node(data);
         this.head = newNode;
         this.tail = newNode;
         this.size++;
         return true;
      }

     this.head = this.head.insert(data);
     this.size++; 

      return true;
   }

   public Type dequeue(){
      Node temp = this.head;

      while(temp.next!= this.tail){
         temp = temp.next;
      }
      this.tail = temp;
      temp = temp.next;
      this.tail.next = null;
      this.size--;

      return temp.getData();

   }

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

   //returns the current size of the queue
   public int size(){
      System.out.println("getting queue size");
      return this.size;
   }

   // console display for testing
   public String toString(){
      Node current = this.head;
      String retString;
      retString ="[ ";
      for(int i = 0 ; i < this.size; i++){
         if(i != 0){
            retString += ",";
         }
         retString += current.getData();

         current = current.getNext();
      }
      retString += " ] ";

      return retString;
   }

   public Type peek(){
      if(this.size == 0){
         return null;
      }
      return this.tail.getData();
   }

   public String getName(){
      return this.name;
   }



   private class Node {
      // data
      private Node next;
      private Type data;

      // constructors
      public Node(Type data){
         this.next = null;
         this.data = data;
      }

      public Node(Type data, Node next) {
         this.next = next;
         this.data = data;
      }

      // inserts new nodes to the front of the list aka the top of the stack
      public Node insert(Type data) {
         return new Node(data,this);
      }

      // returning the next member
      public Node getNext() {
         return next;
      }

      // helps with peek
      public Type getData() {
         return this.data;
      }

      public String toString(){
         return this.data.toString();
      }

   }




   public java.util.Iterator<Type> iterator(){
      return new QueueIterator();
   }

   private class QueueIterator implements Iterator<Type>{

      private Node currentNode;

      public QueueIterator(){
         currentNode = head;
      }

      public boolean hasNext() {
         return currentNode == null;
      }

      public Type next() {
         if(!hasNext()){
            System.out.println("Stack Empty");
            return null;
         }
         Type temp = currentNode.getData();
         currentNode = currentNode.next;
         return temp;
      }

   }

}
Queue.java-
包队列;
导入java.util.Iterator;
公共类队列实现了Iterable{
私有字符串名称;
私有节点头、尾;
私有整数大小;
//队列的基本构造函数
公共队列(字符串名称){
System.out.println(“构造队列”);
this.name=名称;
此值为0.size=0;
}
//列表的前面=队列的末尾
公共布尔排队(类型数据){
System.out.println(“排队”);
if(this.isEmpty()){
Node newNode=新节点(数据);
this.head=newNode;
this.tail=newNode;
这个.size++;
返回true;
}
this.head=this.head.insert(数据);
这个.size++;
返回true;
}
公共类型出列(){
节点温度=this.head;
while(临时下一步!=此尾){
温度=下一个温度;
}
this.tail=温度;
温度=下一个温度;
this.tail.next=null;
这个尺寸--;
返回temp.getData();
}
公共布尔值为空(){
返回this.size==0;
}
//返回队列的当前大小
公共整数大小(){
System.out.println(“获取队列大小”);
返回此.size;
}
//测试用控制台显示器
公共字符串toString(){
节点电流=this.head;
字符串检索字符串;
retString=“[”;
for(int i=0;i
这是我尝试排队的类,我得到了我的错误

//Jukebox.java
package queues;

import java.io.*;
import java.util.*;
import cs1c.*;


public class Jukebox  {
   private Queue<SongEntry> favoritesPL, loungePL, roadTripPL ;

   //Constructor for Jukebox initializes the 3 queues
   public Jukebox(){
      System.out.println("Constructing Jukebox");
     Queue<SongEntry> favoritesPL = new Queue<SongEntry>("favorites");
     Queue<SongEntry> loungePL = new Queue<SongEntry>("loungePL");
     Queue<SongEntry> roadTripPL = new Queue<SongEntry>("roadTripPL");
     System.out.println(favoritesPL.toString());
   }

   //Reads the given file and searches allSongs and if the song is found, it is added
   // to the correct playlist
   public void fillPlaylists(String requestFile, SongEntry[] allSongs){
      ArrayList<String> tempList = new ArrayList<String>();
      try{
         Scanner scanner = new Scanner(new File(requestFile));

         //seperates the info by commas and newLines
         scanner.useDelimiter(",|\\n");
         while(scanner.hasNext()){
              tempList.add(scanner.next());
         }
         scanner.close();
      }
      catch(Exception e){
         System.out.println("File Not Found");
      }

      /* Tests the output of the tempList
      for(int i = 0; i < tempList.size(); i++){
         System.out.println(tempList.get(i));
      }
      */

      for(int i = 1; i < tempList.size(); i++){
       //  for(int j = 0; j < 5000; j++){
            System.out.println("i = " + i  );
            SongEntry tempSongEntry = search(tempList.get(i),allSongs);
            System.out.println(tempSongEntry);
            System.out.println("Returned from search");
            //if the song is not found, don't add null to the playlist
            if(tempSongEntry != null){
               System.out.println("Song Entry not null");
               switch(tempList.get(i-1)){

                  // This is where I keep getting my error. 
                  case "favorites": favoritesPL.enqueue(tempSongEntry);
                                    break;
                  case "lounge":    System.out.println("About to Enqueue");
                                    loungePL.enqueue(tempSongEntry);
                                    System.out.println(loungePL.toString());
                                    break;
                  case "road trip": roadTripPL.enqueue(tempSongEntry);
                                    break;
                  default: break;                  
           //    }
            }
         }
      }

   }


   //helper method to search the json file and return the Song Entry if found
   public SongEntry search(String songName, SongEntry[] allSongs) {
      System.out.println("Searching");

      for(int i = 0; i < allSongs.length; i++){
         if(allSongs[i].getTitle().equals(songName)){
            System.out.println("Found + "+ i);
          //  loungePL.enqueue(allSongs[i]);

            return allSongs[i];
         }
      }

      return null;
   }

   //Accessors
   public Queue<SongEntry> getFavoritePL(){
      return this.favoritesPL;
   }

   public Queue<SongEntry> getLoungePL(){
      return this.loungePL;
   }

   public Queue<SongEntry> getRoadTripPL(){
      return this.roadTripPL;
   }

}
//Jukebox.java
包队列;
导入java.io.*;
导入java.util.*;
进口cs1c。*;
公共类自动存储塔{
私人排队优惠、休息室、路途旅行;
//自动存储塔的构造函数初始化3个队列
公共自动存储塔(){
System.out.println(“构建自动存储塔”);
队列收藏夹SPL=新队列(“收藏夹”);
队列loungePL=新队列(“loungePL”);
队列roadTripPL=新队列(“roadTripPL”);
System.out.println(favoritesPL.toString());
}
//读取给定文件并搜索所有歌曲,如果找到该歌曲,则添加该歌曲
//转到正确的播放列表
公共播放列表(字符串请求文件,歌曲条目[]所有歌曲){
ArrayList tempList=新的ArrayList();
试一试{
扫描仪扫描仪=新扫描仪(新文件(请求文件));
//用逗号和换行符分隔信息
scanner.useDelimiter(“,|\\n”);
while(scanner.hasNext()){
add(scanner.next());
}
scanner.close();
}
捕获(例外e){
System.out.println(“未找到文件”);
}
/*测试圣殿骑士的输出
对于(int i=0;ipublic Jukebox(){
 System.out.println("Constructing Jukebox");
 this.favoritesPL = new Queue<SongEntry>("favorites");
 this.loungePL = new Queue<SongEntry>("loungePL");
 this.roadTripPL = new Queue<SongEntry>("roadTripPL");
 System.out.println(favoritesPL.toString());
  }