0

There are many posts on this topic but I did not get the required answer. Hence, I am here.

I have been getting Notice: Trying to get property of non-object in /opt/lampp/htdocs/amit/crawlnepalstock.php on line 49 error in my php page.

Here is my code

<?php

    include_once('simple_html_dom.php');

    error_reporting(E_ALL);

    $html = file_get_html('http://nepalstock.com/datanepse/index.php');

    $indexarray = array('s_no','stocksymbol', 'LTP', 'LTV', 'point_change', 'per_change', 'open','high', 'low', 'volume','prev_close');

    $stocks = array();

    $maincount = 0;
    $tables = $html->find('table[class=dataTable]');

    $str = $html->plaintext;


    $matches = array();


foreach ($tables[0]->find('tr') as $elementtr) {

    $count = 0;

    $temp = array();

    $anchor = $elementtr->children(1)->find('a',0);

    $splits = preg_split('/=/', $anchor->href); **//line 49**

    $temp['stocksymbol'] = isset($splits[1]) ? $splits[1] : null;
    $temp['fullname'] = $elementtr->children(1)->plaintext;
    $temp['no_of_trans'] = $elementtr->children(2)->plaintext;
    $temp['max_price'] = $elementtr->children(3)->plaintext;
    $temp['min_price'] = $elementtr->children(4)->plaintext;
    $temp['closing_price'] = $elementtr->children(5)->plaintext;
    $temp['total_share'] = $elementtr->children(6)->plaintext;
    $temp['amount'] = $elementtr->children(7)->plaintext;
    $temp['previous_close'] = $elementtr->children(8)->plaintext;
    $temp['difference'] = $elementtr->children(9)->plaintext;

    $stocks[] = $temp;

}


$html->clear();
unset($html);

echo '<pre>';
    print_r($stocks);
echo '</pre>';

?>

I have not included simple_html_dom.php class as it is quite long. Your opinions are very much appreciated. You can find simple_html_dom.php file online in case http://sourceforge.net/projects/simplehtmldom/files/

user3127109
  • 3,421
  • 8
  • 24
  • 33
  • Did you tried to debug array with `print_r()` function? Because this error is about getting value of array field in the wrong format. – user3566301 Jul 08 '14 at 11:36
  • To begin read this: [reference-what-does-this-error-mean-in-php](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983). Where is the line 49 ? Maybe this one `$elementtr->children(1)->find('a',0);`. I guess children return null and show this error. – Debflav Jul 08 '14 at 11:39
  • Please see I have included the line 49 comment in the edited part. – user3127109 Jul 09 '14 at 13:25

2 Answers2

0

You are trying to access property of non-object or from null object. e.g.

    $obj = null;
    echo $boject->first_name // this will produce same error as you are getting.
    // another example may be
     $obj = array();
     echo $obj->first_name; // this will also produce same error.

In code sample Line 49 is not clear so you should check yourself this type of error on line 49.

Vicky
  • 603
  • 5
  • 6
  • Yeah I did var_dump and returned empty string with "". Please view the code i have included the line 49 there!! – user3127109 Jul 09 '14 at 13:26
0

This is happening because there is no longer a td[align="center"] tag found in the google.com document. Perhaps it was there when the code was first written.

So, what the others are saying about a non-object is true, but because the HTML was not found, there is not an object to use the ->plaintext method on.

As of 12/11/2020, if you change the URL found in line 6 of example_basic_selector.php to this: $html = file_get_html('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_td_align_css');

And change this line echo $html->find('td[align="center"]', 1)->plaintext . '


'; to: echo $html->find('td style="text-align:right"', 1)->plaintext. '

';
the error will go away because the text it searches for is found, and thus the method works as intended.

JCDeen
  • 139
  • 10