title: Angular 2 Cannot read property 'x' of null
body: <p>I am learning promises and I am trying to get a simple example to work but I get the error. The promises role is just to check whether a certain name has been entered and produce a boolean error on call back</p>
  
  <blockquote>
    <p>Cannot read property 'shouldBeUnique' of null</p>
  </blockquote>
  
  <p>Here is my component </p>
  
  <pre><code>import {Component, Inject} from '@angular/core';
  import {FormGroup, Validators, FormBuilder} from '@angular/forms';
  import {UsernameValidators} from './usernameValidators'
  
  @Component({
      selector: 'signup-form',
      templateUrl: 'app/signup-form.component.html'
  })
  export class SignUpFormComponent {
  
      form: FormGroup;
  
      constructor(@Inject(FormBuilder) fb: FormBuilder) {
          this.form = fb.group({
              username: ['', Validators.compose([
                  Validators.required,
                  UsernameValidators.cannotContainSpace
              ]), UsernameValidators.shouldBeUnique],
              password: ['', Validators.required]
          })
      }
  
      get username(): any {return this.form.get('username');}
      get password(): any {return this.form.get('password');}
  
  }
  </code></pre>
  
  <p>Here is my component.html</p>
  
  <pre><code>&lt;form [formGroup]="form" (ngSubmit)="signup()"&gt;
      &lt;div class="form-group"&gt;
          &lt;label for="username"&gt;Username&lt;/label&gt;
          &lt;input 
                  id="username"
                  type="text"
                  class="form-control"
                  formControlName="username" placeholder="Username"
                  &gt;
          &lt;div *ngIf="username.touched &amp;&amp; username.errors"&gt;
              &lt;div *ngIf="username.errors.shouldBeUnique" class="alert alert-danger"&gt;This username is already taken&lt;/div&gt;
          &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is my validator class where the promise is being made</p>
  
  <pre><code>import {FormControl} from '@angular/forms';
  
  export class UsernameValidators {
  
      static shouldBeUnique(control: FormControl) {
          return new Promise((resolve, reject) =&gt; {
              setTimeout(function(){
                  if (control.value == "mosh")
                      resolve({ shouldBeUnique: true});
                  else
                      resolve(null);
              }, 1000)
          });
      }
  }
  </code></pre>
  
  <p>Thanks</p>