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 - Retired Efficiency! Custom Serialization

Nhat Anh Dao
Nhat Anh Dao
8,370 Points

Get confused with the code challenge

I don't know what i did wrong but it seem like i stuck at the last step in the code challenge, and it gave me Exception in thread "main" java.lang.OutOfMemoryError: Java heap space error. This is my code

 public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    csr = mSupportReps.poll();
    while(csr == null){
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(csr);
}

Somebody please help me out. Thank you !!

3 Answers

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

The only problem that you have is in the while loop try this

while ((csr = mSupportReps.poll()) == null) { // all in 1 line
    playHoldMusic();
}
Nhat Anh Dao
Nhat Anh Dao
8,370 Points

Now you point that, i just realize that my while loop gonna loop forever. Thanks a lot !

Colby Wise
Colby Wise
3,165 Points

Geovanie, can you please explain what this line of code doing? Why your formatting works but Nhat does not work? P.S. I saw something similar to your formatting in the video and did not understand it which is why i'm asking...

Another way I defined it, which was more intuitive to me was:

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;

    while(mSupportReps  == 0){   //while queue is zero 
      playHoldMusic();
    }

   csr = mSupportReps.poll(); 
   csr.assist(customer);
    mSupportReps.add(csr);
}
Geovanie Alvarez
Geovanie Alvarez
21,500 Points

Well in the Nhat example the problem is going to loop forever because is never update the CustomerSupportRep csr variable in the while loop

 public void acceptCustomer(Customer customer) {
    CustomerSupportRep csr;
    csr = mSupportReps.poll(); // <-- here just update once
    while(csr == null){ // <-- here loop forever with the same csr variable poll data
      playHoldMusic();
    }
    csr.assist(customer);
    mSupportReps.add(csr);
}

that's why i post the code that is update the csr and the same time cheking if is null

while ((csr = mSupportReps.poll()) == null) { // <-- update the csr and the same time cheking if isnull
    playHoldMusic();
}

hope this help you i'm not very good explain things

Hi Geovanie Alvarez, ich just want to let you know, that you actually helped me very much by pointing out, why there was an infinity loop. So, thank you!!!