Merge "am:fix NPE and potential infinite loop problem." into main