Reputation: 2662
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
Upvotes: 3
Views: 29683
Reputation: 1447
The 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:
TypeError: item.sku is null
TypeError: item.sku is null, can't access property "indexOf" of it
TypeError: Cannot read property 'indexOf' of null
at example.html:3
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')
.
Upvotes: 12
Reputation: 3956
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).
Upvotes: 2