Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: null is not an object (evaluating '*')

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

like image 680
I'm nidhin Avatar asked Apr 21 '26 11:04

I'm nidhin


1 Answers

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:

  • 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').

like image 181
Michael Johansen Avatar answered Apr 23 '26 23:04

Michael Johansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!