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 trial

Java Java Data Structures Efficiency! Call Center Queue

Prasoon Shukla
Prasoon Shukla
3,426 Points

Call center quiz

I am getting following error on running the code:

Hello Bob, my name is Canary, how can I assist you. ... Is there anything else I can help you with? Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3236) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140) at java.io.PrintStream.write(PrintStream.java:480) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104) at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185) at java.io.PrintStream.write(PrintStream.java:527) at java.io.PrintStream.print(PrintStream.java:669) at java.io.PrintStream.println(PrintStream.java:806) at CallCenter.playHoldMusic(CallCenter.java:48) at CallCenter.acceptCustomer(CallCenter.java:24) at JavaTester.run(JavaTester.java:110)

The three line of codes added are:

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

     mSupportReps.add(csr);
  }

  public void playHoldMusic() {
    System.out.println("Smooooooth Operator.....");
  }

}

If I declare csr before while loop, I am getting this error. If while loop is written as follows, code is accepted:

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

Why am I getting error on defining csr before while loop and then using it in while loop?

[MOD: amended code block]

Jakob Wozniak
Jakob Wozniak
17,896 Points

I'm not sure about the error exactly, but you need to check to see if a csr is ready inside the while loop. If you never check to see if a csr is ready to take a customer after the initial declaration, no one would ever leave the loop!

The reason it works in your second version of code is because when you have csr = mSupportReps.poll() in loop's parameters, it actually performs that line of code every time it goes through (so it's actually checking to see if csr still equal to null every time it runs through the loop).

I hope this makes sense!

3 Answers

Hi Prasoon,

I amended your code block so it looked a little clearer.

There's a few things to do in this challenge - again, I'm not sure about your error but line 48 triggered something. Let's get your code fixed, though.

Here's the code that works with some comments:

  public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    /*
     * TODO (1) 
     */

// this *will* continually test for 'nullness'
// don't poll the queue, peek it, else you lose the csr!
while(mSupportReps.peek() == null){
  playHoldMusic();
}
    /*
     * TODO (2) 
     */
// got a csr in the queue, so poll it and assign to csr var
// then assist customer
csr = mSupportReps.poll();   
    csr.assist(customer);
    /*
     * TODO (3) 
     */
// we're done ... add the csr back to the queue that's beeing peeked
 mSupportReps.add(csr);    
  }

I hope that helps - shout if not.

Steve.

Prasoon Shukla
Prasoon Shukla
3,426 Points

Yes, thanks. So, my code would look at the availability of csr once and then since at that point of time csr was not available, in while loop that value kind of gets hard coded. Actually we want to check every second of the minute to see if a csr is available or not. Is that correct?

Jakob Wozniak
Jakob Wozniak
17,896 Points

Yes, that's correct! If you don't keep checking it, you'll get stuck in the loop :)