Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'required' of null

Tags:

In the template I have a form, which one part of it has to do with rendering the list of courses:

<form #f="ngForm" (ngSubmit)="submit(f)">  <div class="form-group">   <label for="courseCategory"> Category </label>   <select required ngModel name="courseCategory" #courseCategory="ngModel" id="courseCategory" class="form-control">     <option value=""></option>     <option *ngFor="let category of categories" [value]="category.id"> // line 16       {{category.name}}     </option>   </select>   <div class="alert alert-danger" *ngIf="courseCategory.touched && courseCategory.errors.required">     Course category is required   </div>  </div> </form> 

In the browser when I select a category and press TAB (to move away from the drop-down list), I get this error on Console:

CourseComponent.html:16 ERROR TypeError: Cannot read property 'required' of null at Object.eval [as updateDirectives] (CourseComponent.html:20)

Can you help me finding out what causes this error?

Bootstrap 3.3.7 is already installed in the VS Code.

like image 979
Alpha Bravo Charlie ... Avatar asked May 17 '18 16:05

Alpha Bravo Charlie ...


People also ask

How do you handle Cannot read the property of null?

The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.

What does Cannot read property ID of null mean?

If the error message says "Could not read property 'id' of null" it means that the guild property exists, but it is null.


1 Answers

Errors won't always exist, so you have to define it like this:

<div class="alert alert-danger" *ngIf="courseCategory.touched && courseCategory.errors?.required"> 

With the safe operator "?"

like image 169
David Anthony Acosta Avatar answered Oct 03 '22 23:10

David Anthony Acosta