I've looked everywhere and the advice my professor gave me wasn't as clear as I had hoped. I'm trying to add a certain object to my queue and I keep getting the NPE error. The funny thing is is that the queue works just fine when i have a Queue or Queue, the enqueueing works just fine. It only has a problem when I try to use my specific object type Queue. Could you guys please check out my code and try to help?
My teacher asked me to make sure that I've allocated memory for the queue and that I check if the queue is empty, but I'm sure I'm doing both.
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;
}
}
}
This is class where I try to enqueue and I get my error
//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;
}
}