Loading

Resolving "Cannot call method * of null" or "Cannot read property * of null" errors in B2C Commerce

Publiseringsdato: Apr 11, 2025
Beskrivelse

A common error thrown both in customerror logs and the error logs points to an issue with reading a property or calling a method on an object that is null or empty. This error can be very common, but it can also be resolved and avoided easily.

 

A common error thrown both in customerror logs and the error logs points to an issue with reading a property or calling a method on an object that is null or empty. This error can be very common, but it can also be resolved and avoided easily.


You can see these errors in the customerror logs, but more often than not they will be found in the server error logs. Example errors usually read like:

TypeError: Cannot call method getPrice of null ([Template:product/components/standardprice:${! PriceModel.getPrice().available}]#1)"
TypeError: Cannot read property ID from null ([Template:search/components/jsrefinementsupdate:${pdict.ProductSearchResult.category.ID}]#1)"

If you are seeing these typeErrors in your logs, the root cause of the issue is that the object that you are attempting to read a property from or call a method of, is empty or null.

Løsning

Any typeErrors (where a property or method cannot be read or called because of a null object) will log both the underlying template and the line in the template where the error is being thrown.

Investigate these templates and find the line where the method call or property read is happening. To resolve these errors, a check on the null or empty object has to occur.

For example, using the second error above, the code causing the error could look like this:

<isset name="categoryID" value="${pdict.ProductSearchResult.category.ID}"/>

To avoid the error, add a check that the object being read from is not null or empty:

<isif condition="${!empty(pdict.ProductSearchResult.category)}">
    <isset name="categoryID" value="${pdict.ProductSearchResult.category.ID}"/>
</isif>

This will ensure that there is in fact an object to be read from and further, that the new variable will in fact have a value.

Knowledge-artikkelnummer

000391353

 
Laster
Salesforce Help | Article