Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Efficiency! Call Center Queue

Alex Kasper
Alex Kasper
1,896 Points

Why 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.

CallCenter.java
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.....");
  }

}
CustomerSupportRep.java
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;
  }

}
Customer.java
public class Customer {
  private String mName;

  public Customer(String name) {
    mName = name;
  }

  public String getName() {
    return mName;
  }

}
Alex Kasper
Alex Kasper
1,896 Points

So I figured out where the problem is:

public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;

    csr = mSupportReps.poll();
    while (mSupportReps.poll() == null) {
      playHoldMusic();
      csr = mSupportReps.poll();
    }

    csr.assist(customer);

    mSupportReps.add(csr);
}

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?