Connecting Tech Pros Worldwide Help | Site Map

How to avoid 'Notice: Trying to get property of non-object'

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 18th, 2008, 07:35 AM
vizard
Guest
 
Posts: n/a
Default How to avoid 'Notice: Trying to get property of non-object'

Hi All,
Im new to PHP. In the script I've written, i have the following line:

if($coursesTaught[$i]->empId == $record-
Quote:
>getElementsByTagName('EMPL_ID')->item(0)->nodeValue){
// do something
}

when the if condition executes, i get the foll message: Notice: Trying
to get property of non-object in C:\Program Files\Apache Group
\Apache2\htdocs\ENGR_COURSES\updateCoursesTaught.p hp on line 33

The script works but the message is annoying. I do not want to alter
the error reporting in PHP.ini.
Is there any other way of avoiding this message?

Thanks,
Vin

  #2  
Old March 18th, 2008, 08:05 AM
Joe Scylla
Guest
 
Posts: n/a
Default Re: How to avoid 'Notice: Trying to get property of non-object'

vizard wrote:
Quote:
Hi All,
Im new to PHP. In the script I've written, i have the following line:
>
if($coursesTaught[$i]->empId == $record-
Quote:
>getElementsByTagName('EMPL_ID')->item(0)->nodeValue){
// do something
}
>
when the if condition executes, i get the foll message: Notice: Trying
to get property of non-object in C:\Program Files\Apache Group
\Apache2\htdocs\ENGR_COURSES\updateCoursesTaught.p hp on line 33
>
The script works but the message is annoying. I do not want to alter
the error reporting in PHP.ini.
Is there any other way of avoiding this message?
>
Thanks,
Vin
The error message says that one or both of the objects or their
properties you try to access are not defined (probable null). You can
avoid that notice by checking if the property you try to access is set.

<code>
if(
isset($coursesTaught[$i]->empId) &&
isset($record->getElementsByTagName('EMPL_ID')->item(0)->nodeValue) &&
$coursesTaught[$i]->empId ==
$record->getElementsByTagName('EMPL_ID')->item(0)->nodeValue
)
{
// do something
}
</code>

or set a user-defined error handler suppressing such notices:
http://www.php.net/manual/en/functio...or-handler.php

You should also think about the reason of the error. Is it normal that
you try to access a object or property of an object that is not defined?
Why is the object or property not defined?

Joe
  #3  
Old April 4th, 2008, 04:45 PM
vizard
Guest
 
Posts: n/a
Default Re: How to avoid 'Notice: Trying to get property of non-object'

On Mar 18, 12:58 am, Joe Scylla <joe.scy...@gmail.comwrote:
Quote:
vizard wrote:
Quote:
Hi All,
Im new to PHP. In the script I've written, i have the following line:
>
Quote:
if($coursesTaught[$i]->empId == $record-
Quote:
getElementsByTagName('EMPL_ID')->item(0)->nodeValue){
// do something
}
>
Quote:
when the if condition executes, i get the foll message: Notice: Trying
to get property of non-object in C:\Program Files\Apache Group
\Apache2\htdocs\ENGR_COURSES\updateCoursesTaught.p hp on line 33
>
Quote:
The script works but the message is annoying. I do not want to alter
the error reporting in PHP.ini.
Is there any other way of avoiding this message?
>
Quote:
Thanks,
Vin
>
The error message says that one or both of the objects or their
properties you try to access are not defined (probable null). You can
avoid that notice by checking if the property you try to access is set.
>
<code>
if(
isset($coursesTaught[$i]->empId) &&
isset($record->getElementsByTagName('EMPL_ID')->item(0)->nodeValue) &&
$coursesTaught[$i]->empId ==
$record->getElementsByTagName('EMPL_ID')->item(0)->nodeValue
)
{
// do something
}
</code>
>
or set a user-defined error handler suppressing such notices:http://www.php.net/manual/en/functio...or-handler.php
>
You should also think about the reason of the error. Is it normal that
you try to access a object or property of an object that is not defined?
Why is the object or property not defined?
>
Joe
That worked!! Thanks Joe!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.