# While programming in javascript, getting error "Cannot read property 'style' of null" please help me

**URL:** <https://forum.kirupa.com/t/while-programming-in-javascript-getting-error-cannot-read-property-style-of-null-please-help-me/639486>\
**Category:** programming\
**Created:** [March 11, 2019, 7:18am UTC](https://forum.kirupa.com/t/while-programming-in-javascript-getting-error-cannot-read-property-style-of-null-please-help-me/639486 "2019-03-11T07:18:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikin](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@Vikin](https://forum.kirupa.com/u/Vikin)\
**Post date:** [March 11, 2019, 7:18am UTC](https://forum.kirupa.com/t/while-programming-in-javascript-getting-error-cannot-read-property-style-of-null-please-help-me/639486/1 "2019-03-11T07:18:44Z")

</div>

I have tried this solution, but its not working for me. referred : [help me please with Uncaught TypeError: Cannot read property 'style' of null](http://forum.kirupa.com/t/help-me-please-with-uncaught-typeerror-cannot-read-property-style-of-null/638230).

code I have written:

```auto
<script type="text/javascript">
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function(){
   SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
     OnPostRender: function(ctx) {
       var statusColors = {
          'Pending' : '#9ec4ef',  
          'Reviewed' : '#cfffad',
          'Rejected' : '#42f498' ,
	  'IIQ Updated' : '#d7b5e5'
       };
       var rows = ctx.ListData.Row;
       for (var i=0;i< rows.length;i++)
       {
          var status = rows[i]["Status"];
          var rowId = GenerateIIDForListItem(ctx, rows[i]);
          var row = document.getElementById(rowId); 
          row.style.backgroundColor = statusColors[status];
       }
     }
   }); 
});
</script>

```

thanks in advance

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 11, 2019, 3:23pm UTC](https://forum.kirupa.com/t/while-programming-in-javascript-getting-error-cannot-read-property-style-of-null-please-help-me/639486/2 "2019-03-11T15:23:27Z")

</div>

@Vikin I’ve fixed your code with code formatting. In the future, if you put your code between three backticks (or indent it all by 4 spaces), it will automatically get formatted 🙂

````
```
code here
```

````

As for your problem, its similar to what you linked to, but probably for a different reason. The issue is that `row` is null. It’s null because `rowId` does not match the id of any elemen in your document so a valid element isn’t returned from `getElementById`, null is. Then trying to access `null.style` gives you the error.

You need to figure out why `rowId` is incorrect or why the element by that id doesn’t exist in the document when you expect it to.

---

<div class="post-metadata">

**Author:** ![Vikin](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@Vikin](https://forum.kirupa.com/u/Vikin)\
**Post date:** [March 12, 2019, 6:09am UTC](https://forum.kirupa.com/t/while-programming-in-javascript-getting-error-cannot-read-property-style-of-null-please-help-me/639486/3 "2019-03-12T06:09:50Z")

</div>

@senocular Thanks for your reply.  
Backticks was not issue. I was not able to paste code, that’s why I have removed it.

I am not able to figure out why rowId is giving null value. Do you have alternate way for this.

---

<div class="post-metadata">

**Author:** ![Vikin](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@Vikin](https://forum.kirupa.com/u/Vikin)\
**Post date:** [March 12, 2019, 10:27am UTC](https://forum.kirupa.com/t/while-programming-in-javascript-getting-error-cannot-read-property-style-of-null-please-help-me/639486/4 "2019-03-12T10:27:49Z")

</div>

Problem has bee solved. Just added one line  
if(!=null){row.style.backgroundColor = statusColors[status];}
