V
V
Vladimir2019-02-25 19:28:16
PHP
Vladimir, 2019-02-25 19:28:16

Why does Call to a member function find() on null error occur?

The error constantly occurs on different products, but always on this line $characteristic = $a2->find('.product_short_info_parametr', 0)->find('span');

$html_arr_p1 = $html1->find('.items_catalog_one_item');
    foreach ($html_arr_p1 as $a2) {		
      $characteristic = $a2->find('.product_short_info_parametr', 0)->find('span');
                        {

I don't understand what could be wrong
5c7417965dc40442126518.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arman, 2019-02-25
@Arik

obviously because find returned null, and you treat it as an object, try at least like this

$characteristic = ($t = $a2->find('.product_short_info_parametr', 0)) ? $t->find('span') : null;

Y
Yuri Esin, 2019-02-25
@Exomode

You are trying in your code to work with an object whose reference is null. Either change the logic of the code, or at least set the check for the objects used in the code to null.

R
Roman, 2019-02-25
@procode

Because $a2 is empty
Do a type check

if($a2) {
$characteristic = $a2->find('.product_short_info_parametr', 0)->find('span');
}

Or something like that (isset or something else - I don’t remember offhand how it would be more correct there))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question