Inserting into a Priority queue











up vote
0
down vote

favorite
1












I've been given a task of making a priority queue from scratch without extension programs.



The underlying task in hand is to create a priority queue for an IT ticketing System that allows the IT workers to prioritize which tasks within the company must be completed first . (priority = 1 -> Highest priority and 4 being the lowest).



I'm attempting to do this via a singly linked list.



My issue in hand is once my insertInQueue(Ticket T) function takes the last value in it fails.



The failure return statement is



Exception in thread "main" java.lang.NullPointerException

at Queue.insertInQueue(Queue.java:36)


(the line of code at line 36):



`if( temp.getNextTicket().getPriority() > T.getPriority())



at Main.main(Main.java:21)
(The last object to go into the system)



private Ticket head;
private Ticket tail;




public void insertInQueue(Ticket T){

Ticket temp = head;

if(head == null){ //When no values are in the queue
head = T; //head = Ticket
tail = T; //tail = Ticket

}else if(T.getNextTicket() == null){

tail.setNextTicket(T);
tail = T;


}


else{
while( temp != null ){

if( temp.getNextTicket().getPriority() > T.getPriority()){

T.setNextTicket(temp.getNextTicket());
temp.setNextTicket(T);


}
temp = temp.getNextTicket();


}
}

}


Example of input:



Ticket T8 = new Ticket(8, "Ben_DG", 4);


I've tried a few different things but havn't gotten anywhere. Would anybody be able to help me out?



If you need more of my code let me know and I'll post it up. (just a little concerned some class mates would steal it)



Thanks!










share|improve this question






















  • At some point temp.getNextTicket() is returning null in the while (temp != null) loop. So, you must check for temp.getNextTicket() not being null.
    – KevinO
    Nov 10 at 18:26












  • @Kevin0 Do you mean set while loop to temp.getNextTicket()!=null or do you mean check somewhere before that to not allow it to get to the while loop
    – Ben
    Nov 10 at 20:54






  • 1




    The loop does while (temp != null), but then there is if (temp.getNextTicket().getPriority(). However, based upon other code, temp.getNextTicket() can return null, so the chained call will fail. I didn't read all of the logic, but a simple fix would be if (temp.getNextTicket() != null && temp.getNextTicket().getPriority > T.getPriority()) { .... This approach will fix, I think, the NPE, but there could be other issues.
    – KevinO
    Nov 10 at 20:58















up vote
0
down vote

favorite
1












I've been given a task of making a priority queue from scratch without extension programs.



The underlying task in hand is to create a priority queue for an IT ticketing System that allows the IT workers to prioritize which tasks within the company must be completed first . (priority = 1 -> Highest priority and 4 being the lowest).



I'm attempting to do this via a singly linked list.



My issue in hand is once my insertInQueue(Ticket T) function takes the last value in it fails.



The failure return statement is



Exception in thread "main" java.lang.NullPointerException

at Queue.insertInQueue(Queue.java:36)


(the line of code at line 36):



`if( temp.getNextTicket().getPriority() > T.getPriority())



at Main.main(Main.java:21)
(The last object to go into the system)



private Ticket head;
private Ticket tail;




public void insertInQueue(Ticket T){

Ticket temp = head;

if(head == null){ //When no values are in the queue
head = T; //head = Ticket
tail = T; //tail = Ticket

}else if(T.getNextTicket() == null){

tail.setNextTicket(T);
tail = T;


}


else{
while( temp != null ){

if( temp.getNextTicket().getPriority() > T.getPriority()){

T.setNextTicket(temp.getNextTicket());
temp.setNextTicket(T);


}
temp = temp.getNextTicket();


}
}

}


Example of input:



Ticket T8 = new Ticket(8, "Ben_DG", 4);


I've tried a few different things but havn't gotten anywhere. Would anybody be able to help me out?



If you need more of my code let me know and I'll post it up. (just a little concerned some class mates would steal it)



Thanks!










share|improve this question






















  • At some point temp.getNextTicket() is returning null in the while (temp != null) loop. So, you must check for temp.getNextTicket() not being null.
    – KevinO
    Nov 10 at 18:26












  • @Kevin0 Do you mean set while loop to temp.getNextTicket()!=null or do you mean check somewhere before that to not allow it to get to the while loop
    – Ben
    Nov 10 at 20:54






  • 1




    The loop does while (temp != null), but then there is if (temp.getNextTicket().getPriority(). However, based upon other code, temp.getNextTicket() can return null, so the chained call will fail. I didn't read all of the logic, but a simple fix would be if (temp.getNextTicket() != null && temp.getNextTicket().getPriority > T.getPriority()) { .... This approach will fix, I think, the NPE, but there could be other issues.
    – KevinO
    Nov 10 at 20:58













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I've been given a task of making a priority queue from scratch without extension programs.



The underlying task in hand is to create a priority queue for an IT ticketing System that allows the IT workers to prioritize which tasks within the company must be completed first . (priority = 1 -> Highest priority and 4 being the lowest).



I'm attempting to do this via a singly linked list.



My issue in hand is once my insertInQueue(Ticket T) function takes the last value in it fails.



The failure return statement is



Exception in thread "main" java.lang.NullPointerException

at Queue.insertInQueue(Queue.java:36)


(the line of code at line 36):



`if( temp.getNextTicket().getPriority() > T.getPriority())



at Main.main(Main.java:21)
(The last object to go into the system)



private Ticket head;
private Ticket tail;




public void insertInQueue(Ticket T){

Ticket temp = head;

if(head == null){ //When no values are in the queue
head = T; //head = Ticket
tail = T; //tail = Ticket

}else if(T.getNextTicket() == null){

tail.setNextTicket(T);
tail = T;


}


else{
while( temp != null ){

if( temp.getNextTicket().getPriority() > T.getPriority()){

T.setNextTicket(temp.getNextTicket());
temp.setNextTicket(T);


}
temp = temp.getNextTicket();


}
}

}


Example of input:



Ticket T8 = new Ticket(8, "Ben_DG", 4);


I've tried a few different things but havn't gotten anywhere. Would anybody be able to help me out?



If you need more of my code let me know and I'll post it up. (just a little concerned some class mates would steal it)



Thanks!










share|improve this question













I've been given a task of making a priority queue from scratch without extension programs.



The underlying task in hand is to create a priority queue for an IT ticketing System that allows the IT workers to prioritize which tasks within the company must be completed first . (priority = 1 -> Highest priority and 4 being the lowest).



I'm attempting to do this via a singly linked list.



My issue in hand is once my insertInQueue(Ticket T) function takes the last value in it fails.



The failure return statement is



Exception in thread "main" java.lang.NullPointerException

at Queue.insertInQueue(Queue.java:36)


(the line of code at line 36):



`if( temp.getNextTicket().getPriority() > T.getPriority())



at Main.main(Main.java:21)
(The last object to go into the system)



private Ticket head;
private Ticket tail;




public void insertInQueue(Ticket T){

Ticket temp = head;

if(head == null){ //When no values are in the queue
head = T; //head = Ticket
tail = T; //tail = Ticket

}else if(T.getNextTicket() == null){

tail.setNextTicket(T);
tail = T;


}


else{
while( temp != null ){

if( temp.getNextTicket().getPriority() > T.getPriority()){

T.setNextTicket(temp.getNextTicket());
temp.setNextTicket(T);


}
temp = temp.getNextTicket();


}
}

}


Example of input:



Ticket T8 = new Ticket(8, "Ben_DG", 4);


I've tried a few different things but havn't gotten anywhere. Would anybody be able to help me out?



If you need more of my code let me know and I'll post it up. (just a little concerned some class mates would steal it)



Thanks!







java object priority-queue






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 18:24









Ben

1




1












  • At some point temp.getNextTicket() is returning null in the while (temp != null) loop. So, you must check for temp.getNextTicket() not being null.
    – KevinO
    Nov 10 at 18:26












  • @Kevin0 Do you mean set while loop to temp.getNextTicket()!=null or do you mean check somewhere before that to not allow it to get to the while loop
    – Ben
    Nov 10 at 20:54






  • 1




    The loop does while (temp != null), but then there is if (temp.getNextTicket().getPriority(). However, based upon other code, temp.getNextTicket() can return null, so the chained call will fail. I didn't read all of the logic, but a simple fix would be if (temp.getNextTicket() != null && temp.getNextTicket().getPriority > T.getPriority()) { .... This approach will fix, I think, the NPE, but there could be other issues.
    – KevinO
    Nov 10 at 20:58


















  • At some point temp.getNextTicket() is returning null in the while (temp != null) loop. So, you must check for temp.getNextTicket() not being null.
    – KevinO
    Nov 10 at 18:26












  • @Kevin0 Do you mean set while loop to temp.getNextTicket()!=null or do you mean check somewhere before that to not allow it to get to the while loop
    – Ben
    Nov 10 at 20:54






  • 1




    The loop does while (temp != null), but then there is if (temp.getNextTicket().getPriority(). However, based upon other code, temp.getNextTicket() can return null, so the chained call will fail. I didn't read all of the logic, but a simple fix would be if (temp.getNextTicket() != null && temp.getNextTicket().getPriority > T.getPriority()) { .... This approach will fix, I think, the NPE, but there could be other issues.
    – KevinO
    Nov 10 at 20:58
















At some point temp.getNextTicket() is returning null in the while (temp != null) loop. So, you must check for temp.getNextTicket() not being null.
– KevinO
Nov 10 at 18:26






At some point temp.getNextTicket() is returning null in the while (temp != null) loop. So, you must check for temp.getNextTicket() not being null.
– KevinO
Nov 10 at 18:26














@Kevin0 Do you mean set while loop to temp.getNextTicket()!=null or do you mean check somewhere before that to not allow it to get to the while loop
– Ben
Nov 10 at 20:54




@Kevin0 Do you mean set while loop to temp.getNextTicket()!=null or do you mean check somewhere before that to not allow it to get to the while loop
– Ben
Nov 10 at 20:54




1




1




The loop does while (temp != null), but then there is if (temp.getNextTicket().getPriority(). However, based upon other code, temp.getNextTicket() can return null, so the chained call will fail. I didn't read all of the logic, but a simple fix would be if (temp.getNextTicket() != null && temp.getNextTicket().getPriority > T.getPriority()) { .... This approach will fix, I think, the NPE, but there could be other issues.
– KevinO
Nov 10 at 20:58




The loop does while (temp != null), but then there is if (temp.getNextTicket().getPriority(). However, based upon other code, temp.getNextTicket() can return null, so the chained call will fail. I didn't read all of the logic, but a simple fix would be if (temp.getNextTicket() != null && temp.getNextTicket().getPriority > T.getPriority()) { .... This approach will fix, I think, the NPE, but there could be other issues.
– KevinO
Nov 10 at 20:58












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Since you haven't provided the rest of the code for what set and get next ticket, I can't tell for sure, but logically at some point you are trying to get a NULL value with your get method which is giving you the error. Check all your conditionals properly and see if you aren't trying to get a NULL value.



That should be the main issue and should fix it






share|improve this answer





















  • public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
    – Ben
    Nov 10 at 23:26












  • Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
    – MrCode
    Nov 11 at 0:42










  • I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
    – MrCode
    Nov 11 at 0:48













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242078%2finserting-into-a-priority-queue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Since you haven't provided the rest of the code for what set and get next ticket, I can't tell for sure, but logically at some point you are trying to get a NULL value with your get method which is giving you the error. Check all your conditionals properly and see if you aren't trying to get a NULL value.



That should be the main issue and should fix it






share|improve this answer





















  • public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
    – Ben
    Nov 10 at 23:26












  • Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
    – MrCode
    Nov 11 at 0:42










  • I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
    – MrCode
    Nov 11 at 0:48

















up vote
0
down vote













Since you haven't provided the rest of the code for what set and get next ticket, I can't tell for sure, but logically at some point you are trying to get a NULL value with your get method which is giving you the error. Check all your conditionals properly and see if you aren't trying to get a NULL value.



That should be the main issue and should fix it






share|improve this answer





















  • public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
    – Ben
    Nov 10 at 23:26












  • Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
    – MrCode
    Nov 11 at 0:42










  • I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
    – MrCode
    Nov 11 at 0:48















up vote
0
down vote










up vote
0
down vote









Since you haven't provided the rest of the code for what set and get next ticket, I can't tell for sure, but logically at some point you are trying to get a NULL value with your get method which is giving you the error. Check all your conditionals properly and see if you aren't trying to get a NULL value.



That should be the main issue and should fix it






share|improve this answer












Since you haven't provided the rest of the code for what set and get next ticket, I can't tell for sure, but logically at some point you are trying to get a NULL value with your get method which is giving you the error. Check all your conditionals properly and see if you aren't trying to get a NULL value.



That should be the main issue and should fix it







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 22:40









MrCode

162




162












  • public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
    – Ben
    Nov 10 at 23:26












  • Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
    – MrCode
    Nov 11 at 0:42










  • I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
    – MrCode
    Nov 11 at 0:48




















  • public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
    – Ben
    Nov 10 at 23:26












  • Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
    – MrCode
    Nov 11 at 0:42










  • I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
    – MrCode
    Nov 11 at 0:48


















public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
– Ben
Nov 10 at 23:26






public Ticket getNextTicket(){ return this.nextTicket; } public void setNextTicket(Ticket nextTicket){ this.nextTicket = nextTicket; }
– Ben
Nov 10 at 23:26














Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
– MrCode
Nov 11 at 0:42




Yes, it's as I thought, a simple assignment and return type functions, so probably at some point in your insertInQueue function you are referencing a NULL variable
– MrCode
Nov 11 at 0:42












I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
– MrCode
Nov 11 at 0:48






I see where the problem is @Ben, In your while statement you are looping all the way to the node before it becomes NULL, so after that if you execute temp.getNextTicket() function that will reference to a NULL variable hence giving you the error.
– MrCode
Nov 11 at 0:48




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242078%2finserting-into-a-priority-queue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma