Well, I can't figure out exactly what the problem is but the reason probably is YCM is not ready while loading Nerdtree. So I add a if condition to check if YCM is ready.
function! YCMStatusLine() abort
let l:errCounts = youcompleteme#GetErrorCount()
let l:wrnCounts = youcompleteme#...