单链表Queue中的java.lang.NullPointerException

时间:2016-04-30 06:04:11

标签: java nullpointerexception linked-list queue

我到处寻找,教授给我的建议并不像我希望的那么明确。我正在尝试将某个对象添加到队列中,并且我不断收到NPE错误。有趣的是,当我有队列或队列时,队列工作得很好,排队工作正常。当我尝试使用我的特定对象类型Queue时,它只会出现问题。你们可以查看我的代码并尝试提供帮助吗?

我的老师让我确保我已经为队列分配了内存,并且我检查队列是否为空,但我确定我正在做两件事。

    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;
      }

   }

}

这是我尝试入队的类,我收到了错误

//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;
   }

}

1 个答案:

答案 0 :(得分:2)

您的类级别队列永远不会被初始化,因为您在构造函数中声明了新的队列。

更改为:

public 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());
  }