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 trialNhat Anh Dao
8,370 PointsGet 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
21,500 PointsThe only problem that you have is in the while loop try this
while ((csr = mSupportReps.poll()) == null) { // all in 1 line
playHoldMusic();
}
Geovanie Alvarez
21,500 PointsWell 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
felixwin
14,939 PointsHi 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!!!
Nhat Anh Dao
8,370 PointsNhat Anh Dao
8,370 PointsNow you point that, i just realize that my while loop gonna loop forever. Thanks a lot !
Colby Wise
3,165 PointsColby Wise
3,165 PointsGeovanie, 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: