Inserting into a Priority queue
up vote
0
down vote
favorite
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())
(The last object to go into the system)
at Main.main(Main.java:21)
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
add a comment |
up vote
0
down vote
favorite
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())
(The last object to go into the system)
at Main.main(Main.java:21)
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
At some pointtemp.getNextTicket()
is returning null in thewhile (temp != null)
loop. So, you must check fortemp.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 doeswhile (temp != null)
, but then there isif (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 beif (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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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())
(The last object to go into the system)
at Main.main(Main.java:21)
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
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())
(The last object to go into the system)
at Main.main(Main.java:21)
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
java object priority-queue
asked Nov 10 at 18:24
Ben
1
1
At some pointtemp.getNextTicket()
is returning null in thewhile (temp != null)
loop. So, you must check fortemp.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 doeswhile (temp != null)
, but then there isif (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 beif (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
add a comment |
At some pointtemp.getNextTicket()
is returning null in thewhile (temp != null)
loop. So, you must check fortemp.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 doeswhile (temp != null)
, but then there isif (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 beif (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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
At some point
temp.getNextTicket()
is returning null in thewhile (temp != null)
loop. So, you must check fortemp.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 isif (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 beif (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