0

I'm trying to create Angular Material Chips as shown on the site, but I keep getting this error about the array being null.

enter image description here

Here's the component

import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { IStatement } from 'src/Interface/ICorporateStatement';
import { StatementService } from '../services/statement.service';
import { MatChipInputEvent } from '@angular/material/chips';
import {COMMA, ENTER} from '@angular/cdk/keycodes';

export interface Tag{
  corporate_statement_link_id: number;
  name: string;
}


@Component({
  selector: 'app-statement-detail',
  templateUrl: './statement-detail.component.html',
  styleUrls: ['./statement-detail.component.css']
})
export class StatementDetailComponent implements OnInit {

  statement: IStatement;
  id: number;

  tags: Tag[] = [];
  
  visible = true;
  selectable = true;
  removable = true;
  addOnBlur = true;
  readonly separatorKeysCodes: number[] = [ENTER, COMMA];
  
  constructor(private statementService: StatementService, 
    private router:ActivatedRoute) { }

  ngOnInit(): void {

    this.tags = [
      { corporate_statement_link_id: 1, name: 'EDI'}
    ];
  
    console.log("Tags: ", this.tags);

    this.router.queryParams.subscribe(param => {
      this.id = param.id;
      this.getStatement(this.id);
    });
  }

  addTag(event: MatChipInputEvent): void {

    console.log(this.tags);
    const input = event.input;
    const value = event.value;
    console.log("Input: ", input);
    console.log("Value: ", value);
    console.log("Tags: ", this.tags);

    this.tags.push({corporate_statement_link_id: this.statement.corporate_statement_link_id, name: value.trim()});

    // // Add our fruit
    // if ((value || '').trim()) {
    //   this.fruits.push({name: value.trim()});
    // }

    // // Reset the input value
    // if (input) {
    //   input.value = '';
    // }
  }

  removeTag(tag: Tag): void {
    console.log("removing");
    // const index = this.fruits.indexOf(fruit);

    // if (index >= 0) {
    //   this.fruits.splice(index, 1);
    // }
  }
  // get statement
  getStatement(id){
    this.statementService.getStatement(id).subscribe(data => {
      this.statement = <IStatement>data[0];

      //get tags
      this.statementService.getTags(this.statement.corporate_statement_link_id)
        .subscribe(tag => {
          this.tags = <Tag[]>tag;
        })

    }, error => {
      console.log(error);
    });

  }

}

I've refactored the code and moved things here and there, but still can't figure out why the array is still null.

2
  • 1
    Assumption is that this.tags = <Tag[]>tag; is resulting in this.tags being null and then this.tags.push() fails. Commented Nov 19, 2020 at 4:03
  • Could you please elaborate? I don't understand. Thanks Commented Nov 19, 2020 at 4:06

1 Answer 1

2

It looks like this code is setting this.tags to null.

.subscribe(tag => {
          this.tags = <Tag[]>tag;
        })

It may be an issue with your <Tag[]> cast, or maybe the data coming back is null?

if it is expected you could replace any null value here with an empty array like this:

.subscribe(tag => {
          this.tags = <Tag[]>tag || [];
        })

and see if that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

That is absolutely right. I just figured that out too, and found your answer.
Won't be an issue with the cast as that is compile time only, it will be that the result was null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.