An error occurred while trying to access properties of null, specifically the `_rawValidators` property

I recently upgraded an app from angular 8 to angular14 and encountered a problem with a form array.

The error message I'm seeing is cfs-detail.component.html:13 ERROR TypeError: Cannot read properties of null (reading '_rawValidators'). It seems to be related to the FormBuilder in my code.

When the array loads, it does not render correctly. Initially, it looks off but improves when tabbing through the controls until it eventually displays correctly.

https://i.stack.imgur.com/n8X5d.png

Prior to the Angular upgrade, the CSS grid layout was correct. However, after making changes to fix initial value errors, the layout was disrupted.

https://i.stack.imgur.com/hc2Yf.png

Below is the HTML template for reference:

<ng-template #loading>
    <div   >loading...</div>
    <div *ngIf="!year">please pass in year</div>
    <div *ngIf="!showBy">please pass in periodChoice!</div>
    <div *ngIf="!myPeriods">periods have not loaded</div>

</ng-template>  
 
 
<span *ngIf="showBy ; else loading">
    <span *ngIf="year ; else loading">
        <div class="year">
            <button (click)="toggleForm()" id="btnToggle">{{toggleFormText}}</button>
            
            {{year}}   
            
        </div>
       
        <div *ngIf="myPeriods   as periods  ; else loading "    class="cfs-12month-grid {{showBy}} y{{year}}">
            <ng-container [formGroup]="myFormGroup">
                 
                <ng-container  formArrayName="periods" >
                   

                    <ng-container *ngFor="let perFrm   of periods12.controls ;let i = index;" formGroupName="i"   >
                         
                        
                        <p  class="fieldlabel cfs {{showBy}} {{monthShortName(myPeriods[i])}}">{{shortDate(myPeriods[i]) }} 
                        </p>
                            
                            
                        <input  
                        class="fieldvalue cfs {{showBy}} {{ monthShortName(myPeriods[i]) }}"  
                        type="text" value="{{roundedAmount(myPeriods[i])}}" 
                        formControlName="Amount"
                     />  
                        
                        
                    </ng-container>
                </ng-container>
            </ng-container>    

        </div>
    </span>
</span> 

And here is the component code snippet:

import { Component, OnInit , AfterViewInit
  , Input, Output, SimpleChanges 
  , ElementRef, EventEmitter, ViewChild} from '@angular/core';
 import { AbstractControl  , FormBuilder, FormGroup   
    , Validators
    , RequiredValidator, MaxLengthValidator, MinLengthValidator, FormControl, FormArray
} from '@angular/forms';
import { Observable   } from 'rxjs';
import { Tools } from '../../tools';
import { CashFlowStringPeriod } from '../cash-flow-string-period';
import { cashFlowStringService } from '../services/cash-flow-string.service';
import { cashFlowStringPeriodService } from '../services/cash-flow-string-period.service';

// Component details here...

Answer №1

Hopefully this information will be beneficial for someone in the future. Unfortunately, there were no results found when searching for the question's title...

I made a simple yet frustrating mistake.

The value of formGroupName should not be 'i', but instead, it should be equal to the variable i.

Therefore,

<ng-container *ngFor="let perFrm   of periods12.controls ;let i = index;" formGroupName="i"   >

should be enclosed in square brackets like so:

<ng-container *ngFor="let perFrm   of periods12.controls ;let i = index;" [formGroupName]="i"   >

I don't recall exactly which error messages prompted me to modify the *ngFor statements. I believe it had something to do with variables now needing initialization.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

react-navigation hook for navigating

Currently, I am utilizing the react-navigation hook and instead of repeating the hook in various components, my goal is to pass navigation as a prop. const navigation = useNavigation(); ... <MyButton resetLocation={resetLocation} navigation= ...

Leverage the power of an Angular component for repeated

Attempting to recycle an Angular component within the given tree structure. The desired component, existing-savings, is located in transfer-guide. Trying to utilize this component in retirement-contributions-information. What steps should be taken to ach ...

Setting up types for variables in Angular 2 componentsHere is an

I have a model that consists of multiple properties, and I aim to set all these properties with a default value of either empty or null. Here is an example of my Model: export class MyModel { name: string; jerseyNumber: number; etc... } In m ...

Master the art of iterating through an Object in TypeScript

I need help with looping through an Object in TypeScript. While the following examples work in JavaScript, I understand why they pose a problem in TypeScript. Unfortunately, I am struggling to find the correct approach to solve this issue. Am I approaching ...

Angular 14 - Issue with passing values through props - Error: Uncaught (in promise): InvalidCharacterError occurs when attempting to set attribute with 'setAttribute' method

I am a beginner with Angular and encountering an issue when trying to pass props from a parent component to a child component. The specific error I am facing is related to an invalid attribute name while using Angular version 14.2.5. core.mjs:7635 ERROR ...

Creating an HTML template in an Angular service and utilizing it as an HTMLTemplateRef

I'm currently working on a major project that has a specific requirement. As part of this project, I am utilizing a library which allows me to open dialog (modal) popups. In order to do so, I need to configure some options for the modal opening proce ...

Tips for enhancing a table with extra functionality in Angular 6

Hi there! I've created a table using Angular 6 and now I'm looking to enhance it with some extra functionality: Implement an overall table search feature Allow users to sort the table by columns Add a "Page x of n" option for pagination I woul ...

Transforming TimeZone Date Object without Altering Time

I am looking to change the time zone of a date from one timezone to another. For example: "Sun May 01 2019 00:00:00 GMT+0530 (India Standard Time)" is the current date object. I want to convert this date based on a specific timeZone offset, let's say ...

Angular 6: Endlessly Scroll in Both Directions with Containers

Does anyone know of a library for angular 6 that allows for the creation of a scrollable container that can be scrolled indefinitely in both directions? The content within this container would need to be generated dynamically through code. For example, ...

Steps for utilizing field labels to transmit values in Protractor

Could someone offer guidance on how to send values using field labels? I understand that it's generally not recommended to use labels for sending values since they can change, but in my case, the labels remain constant. I have attached screenshots of ...

how do I address the issue of Property 'navigation' not being found in type 'Readonly<{}> &'?

Here are two snippets of code that I am having trouble with: CustomHeader.tsx import { View, StyleSheet, Button } from 'react-native'; import { NavigationScreenProps } from 'react-navigation'; import Icon from 'react-native-vecto ...

Retrieving Vue component properties as a data type

I'm facing a dilemma with my Vue components. I want to extract the props from one component and use them as a type instead of a value in another component. Specifically, I have a component where I need to take in an array of props from a different com ...

Unexpected outcome in Angular unit testing

I am new to Angular unit testing using Jasmine and Karma. I have created a simple component for testing purposes. Here is my component code: import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-input&apos ...

It appears that React Native's absolute paths are not functioning as expected

I have been attempting to set up React Native with absolute paths for easier imports, but I am having trouble getting it to work. Here is my tsconfig.json: { "compilerOptions": { "allowJs": true, "allowSynthetic ...

Disabling ESLint errors is not possible within a React environment

I encountered an eslint error while attempting to commit the branch 147:14 error Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions I'm struggling to identify the issue in the code, even ...

The URL "http://localhost:8100" has been restricted by the CORS policy, as it lacks the necessary 'Access-Control-Allow-Origin' header on the requested resource

`The CORS policy has blocked access to the XMLHttpRequest at 'http://localhost/phpfile/leave-option.php' from the origin 'http://localhost:8100'. This is due to the absence of the 'Access-Control-Allow-Origin' header on the re ...

Bringing in the component's individual module

I encountered the error message in my Angular application - Can't bind to 'formGroup' since it isn't a known property of 'form' - and managed to resolve it by including the import import { AddEditModule } from './add.edit ...

Error: Unable to convert <> to a Fragment due to a conflict with multiple versions of prosemirror-model being loaded

While utilizing the ngx-editor in my application, everything was working smoothly until recently when I encountered an issue only on my local setup. The problem arises when attempting to press Enter to create a new line. Typing and using the toolbar still ...

You must provide a secret or key in order to use the JwtStrategy

I have encountered the following error and I am unsure of its cause. Can you assist me? ERROR [ExceptionHandler] JwtStrategy requires a secret or key TypeError: JwtStrategy requires a secret or key at new JwtStrategy (C:\Users\wapg2\OneDriv ...

Pay attention to the input field once the hidden attribute is toggled off

In attempting to shift my attention to the input element following a click on the edit button, I designed the code below. The purpose of the edit is to change the hidden attribute to false. Here's what I attempted: editMyLink(i, currentState) { ...