Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlex Kasper
1,896 PointsWhy am I unable to add a variable into a queue?
I'm unable to add the csr into the queue and I'm slightly confused as to why.
The error code that I'm getting is "Bummer! java.lang.NullPointerException (Look around CallCenter.java line 32)."
Here's what the output looks like: Hello Bob, my name is Canary, how can I assist you. ... Is there anything else I can help you with? Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... Smooooooth Operator..... java.lang.NullPointerException at CallCenter.acceptCustomer(CallCenter.java:32) at JavaTester.run(JavaTester.java:110) at JavaTester.main(JavaTester.java:41)
It looks like to me that the assist function and the while loop are both functioning but the add function is not. I've checked the java8 documentation and various googling's and it looks correct so not really sure what's going on.
import java.util.ArrayDeque;
import java.util.Queue;
public class CallCenter {
Queue<CustomerSupportRep> mSupportReps;
public CallCenter(Queue<CustomerSupportRep> queue) {
mSupportReps = queue;
}
public void acceptCustomer(Customer customer) {
CustomerSupportRep csr;
/********************************************
* TODO (1)
* Wait until there is an available rep in the queue.
* While there is not one available, playHoldMusic
* HINT: That while assignmentcheck loop syntax we used to
* read files seems pretty similar
********************************************
*/
csr = mSupportReps.poll();
while (mSupportReps.poll() == null) {
playHoldMusic();
}
/********************************************
* TODO (2)
* After we have assigned the rep, call the
* assist method and pass in the customer
********************************************
*/
csr.assist(customer);
/********************************************
* TODO (3)
* Since the customer support rep is done with
* assisting, put them back into the queue.
********************************************
*/
mSupportReps.add(csr);
}
public void playHoldMusic() {
System.out.println("Smooooooth Operator.....");
}
}
import java.util.List;
import java.util.ArrayList;
public class CustomerSupportRep {
private String mName;
private List<Customer> mAssistedCustomers;
public CustomerSupportRep(String name) {
mName = name;
mAssistedCustomers = new ArrayList<Customer>();
}
public void assist(Customer customer) {
System.out.printf("Hello %s, my name is %s, how can I assist you.%n",
customer.getName(),
mName);
System.out.println("...");
System.out.println("Is there anything else I can help you with?");
mAssistedCustomers.add(customer);
}
public List<Customer> getAssistedCustomers() {
return mAssistedCustomers;
}
}
public class Customer {
private String mName;
public Customer(String name) {
mName = name;
}
public String getName() {
return mName;
}
}
Alex Kasper
1,896 PointsAlex Kasper
1,896 PointsSo I figured out where the problem is:
My question is why does csr = mSupportReps.poll() need be used in the while loop? I know that csr needs to be initialized before the loop for it to be recognized. Would it be that the while loop is constantly assigning a new variable to csr?