Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular 12 NgOnChanges Cannot Read Property of null

I have a child component that is reading the changes of a selection from a drop down list. I’m trying to set local variables on the component based on when the selection changes and it works unless I clear out the selection, then I get:

Cannot read properties of null (reading ‘carrierStatusId’)

The code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public carrierCorporateStatusEnum = CarrierCorporateApprovalStatus;
public carrierStatusEnum = CarrierStatus;
public carrierAddendumStatusEnum = CarrierAddendumStatus;

ngOnChanges(changes: SimpleChanges) {

if (changes.carrier.currentValue != changes.carrier.previousValue )
{
  this.carrier = changes.carrier.currentValue;
  this.corporateStatus = changes.corporateStatus.currentValue;
  this.carrierStatus = changes.carrier.currentValue.carrierStatusId;
  this.carrierAddendumStatus = changes.carrier.currentValue.addendumStatus;
}

}

I know there’s a way to check for null but I can’t quite figure it out. Think I’m making this harder than it is.

>Solution :

this.carrierStatus = changes.carrier.currentValue?.carrierStatusId;  

Add the "?" before .carrierStatusId.

But take care that this.carrierStatus will be null.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading