For the below code:
var item = cartModel.getlist()[index];
if((item.isDepo()) {
// Some code
} else if(!permission.hasPermissionToVoidSKU()) {
// Some code
} else if(item.sku.indexOf(mposConstants.RESTOCK_FEE_SKU) > -1){
// Some code
}
I'm getting this error:
TypeError: null is not an object (evaluating 'item.sku.indexOf')
If item object is null, the error is something different (see below). In what scenario will this error be thrown?
Update:
If item.sku
is null, the error is:
[FATAL] [] [-] ["TypeError: Cannot read property 'indexOf' of null
If item
is null, the error is:
[FATAL] [] [-] ["TypeError: Cannot read property 'isDepo' of null
For the below code:
var item = cartModel.getlist()[index];
if((item.isDepo()) {
// Some code
} else if(!permission.hasPermissionToVoidSKU()) {
// Some code
} else if(item.sku.indexOf(mposConstants.RESTOCK_FEE_SKU) > -1){
// Some code
}
I'm getting this error:
TypeError: null is not an object (evaluating 'item.sku.indexOf')
If item object is null, the error is something different (see below). In what scenario will this error be thrown?
Update:
If item.sku
is null, the error is:
[FATAL] [] [-] ["TypeError: Cannot read property 'indexOf' of null
If item
is null, the error is:
Share Improve this question edited Jul 15, 2019 at 10:49 Icarus 1,6477 gold badges20 silver badges34 bronze badges asked Dec 5, 2017 at 14:07 I'm nidhinI'm nidhin 2,6626 gold badges39 silver badges65 bronze badges 6 | Show 1 more comment[FATAL] [] [-] ["TypeError: Cannot read property 'isDepo' of null
2 Answers
Reset to default 12The reason for the different error messages is quite simply that they are produced by different browsers. The error is the same (sku
on the object item
is null
).
Given the following code
<script>
var item = {sku: null};
item.sku.indexOf("");
</script>
here is some error messages for different browsers:
- Firefox:
TypeError: item.sku is null
- Firefox Developer Edition:
TypeError: item.sku is null, can't access property "indexOf" of it
- Opera:
TypeError: Cannot read property 'indexOf' of null at example.html:3
- Safari:
TypeError: null is not an object (evaluating 'item.sku.indexOf')
To get the error message you have gotten, item
must be defined as an object, and sku
must be set to null
. If sku
were undefined
, you would have gotten an error message like this (Safari): TypeError: undefined is not an object (evaluating 'item.sku.indexOf')
. If item
was null
, you would have gotten something like this: TypeError: null is not an object (evaluating 'item.sku')
.
Based on this answer and others, it sounds like you're getting that error because a function is called before the DOM element it references or acts upon has been loaded.
In the snippet of code you provided, I don't see a direct reference to any DOM elements, but I would suggest calling your script after your HTML has finished rendering (i.e. by putting any <script>
tags at the end of your HTML, or by using a $(document).ready()
call if you use jQuery).
item
oritem.sku
is null, check them in console – Aleksey Solovey Commented Dec 5, 2017 at 14:12item
oritem.sku
areundefined
instead ofnull
? – freginold Commented Dec 5, 2017 at 14:45