TypeError: Cannot read properties of null (reading 'rows')

L_Leggett
Tera Contributor

Hello! 

 

I seem to be getting an error on a Catalog Task of 'TypeError: Cannot read properties of null (reading 'rows')' - 

This only shows after loading and references the error being in a certain client script.

 

I have narrowed the issue down to two lines in the script -
var rowcnt = table.rows.length;

var colcnt = table.rows[0].cells.length;

 

When these two lines are commented out, the error doesn't show upon reloading the page but when they are active, the error is present. 

Is there something I may be missing with these two lines? 
Unfortunately I cannot add more of the code as this is for work purposes, apologies! 

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @L_Leggett ,

 

The error you're encountering, "TypeError: Cannot read properties of null (reading 'rows')," typically indicates that the `table` object is null or undefined when you are trying to access its properties. Here are a few things you can check:

 

1. **Ensure the Table Element Exists:**

   - Confirm that the table you are trying to access actually exists on the page.

   - Make sure the script is running after the table has been rendered in the DOM.

 

2. **Check if the Element is Found:**

   - Before accessing its properties, check if the `table` element is found properly. You can do this using a conditional check:

 

     

     var table = document.getElementById('your_table_id'); // Replace 'your_table_id' with the actual ID of your table

 

     if (table) {

         var rowcnt = table.rows.length;

         var colcnt = table.rows[0].cells.length;

         // Rest of your code

     } else {

         console.error('Table not found');

     }

     

 

3. **Ensure Rows and Cells Exist:**

   - Confirm that the table has rows and cells before trying to access them:

 

     

     var table = document.getElementById('your_table_id');

 

     if (table && table.rows.length > 0 && table.rows[0].cells.length > 0) {

         var rowcnt = table.rows.length;

         var colcnt = table.rows[0].cells.length;

         // Rest of your code

     } else {

         console.error('Table, rows, or cells not found');

     }

     

 

4. **Check for Asynchronous Loading:**

   - If the table is loaded asynchronously, make sure your script runs after the table is fully loaded. 

 

By adding these checks, you can ensure that the `table` object is properly initialized before attempting to access its properties, and it should help prevent the "TypeError" you're encountering.

 

Thanks,

Danish

 

View solution in original post

4 REPLIES 4

Abbas_5
Tera Sage
Tera Sage

Hello @L_Leggett,
Please refer to the link below:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0690722

 

Mark my correct and helpful, if it is helpful and please hit the thumbs-up button to mark it as the correct solution.
Thanks & Regards,
Abbas Shaik

Danish Bhairag2
Tera Sage
Tera Sage

Hi @L_Leggett ,

 

The error you're encountering, "TypeError: Cannot read properties of null (reading 'rows')," typically indicates that the `table` object is null or undefined when you are trying to access its properties. Here are a few things you can check:

 

1. **Ensure the Table Element Exists:**

   - Confirm that the table you are trying to access actually exists on the page.

   - Make sure the script is running after the table has been rendered in the DOM.

 

2. **Check if the Element is Found:**

   - Before accessing its properties, check if the `table` element is found properly. You can do this using a conditional check:

 

     

     var table = document.getElementById('your_table_id'); // Replace 'your_table_id' with the actual ID of your table

 

     if (table) {

         var rowcnt = table.rows.length;

         var colcnt = table.rows[0].cells.length;

         // Rest of your code

     } else {

         console.error('Table not found');

     }

     

 

3. **Ensure Rows and Cells Exist:**

   - Confirm that the table has rows and cells before trying to access them:

 

     

     var table = document.getElementById('your_table_id');

 

     if (table && table.rows.length > 0 && table.rows[0].cells.length > 0) {

         var rowcnt = table.rows.length;

         var colcnt = table.rows[0].cells.length;

         // Rest of your code

     } else {

         console.error('Table, rows, or cells not found');

     }

     

 

4. **Check for Asynchronous Loading:**

   - If the table is loaded asynchronously, make sure your script runs after the table is fully loaded. 

 

By adding these checks, you can ensure that the `table` object is properly initialized before attempting to access its properties, and it should help prevent the "TypeError" you're encountering.

 

Thanks,

Danish

 

Thank you @Danish Bhairag2 ! It looks like the table wasn't being found! 

Ankur Bawiskar
Tera Patron
Tera Patron

@L_Leggett 

what's the complete client script dong?

It seems you are iterating some rows

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader