2016-04-30 72 views
-1

我到处寻找,我的教授给我的建议并不像我希望的那么清晰。我试图添加一个特定的对象到我的队列中,并且一直收到NPE错误。有趣的是,当我有队列或队列时队列工作得很好,排队工作得很好。当我尝试使用我的特定对象类型队列时,它只有一个问题。你们可以看看我的代码并尝试帮忙吗?单链表中的java.lang.NullPointerException队列

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

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

} 
+0

@MitchWeaver这是在评论中。请看代码。 – user2494817

+0

请勿基本删除整篇文章的内容。这对未来的访客可能很有价值。如果您认为自己会遇到问题,请提问SO从您的帐户中解除此问题。通过联系他们来做到这一点 –

回答

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

天啊,谢谢! :D你是一个救生员 –

+0

是啊 - 它的经典;) –