Object reference not set to an instance of an object error

Discussion about the open-source Phalanger [?] project.

Object reference not set to an instance of an object error

Postby compucoder » August 16th, 2012, 12:36 pm

I am trying to run our web application on Phalanger and I didn't expect it work without some work but I have hit a snag I can't seem to pass. When I create a mysql connection and then try to save it in the class I get the error shown in the subject.

The error happens the instant I try and save the mysql connection into the class property.

Here are the 2 lines that I am using:

public function __construct() {
$conn = mysql_connect($host, $username, $password);
$this->_conn = $conn // Dies here with the error instantly.
}

I am using the mysql extension from your sister project. The connection itself works great - if I just try and use the connection to issue queries it gives back rows as expected. Try and save the connection in the public (or private) property $_conn and things go bad.

Thanks for any help.
compucoder
 
Posts: 12
Joined: August 16th, 2012, 12:29 pm

Re: Object reference not set to an instance of an object err

Postby Jakub Misek » August 16th, 2012, 2:10 pm

Hi,

I believe this has nothing to do with MySQL extension.

Do you have a complete test case? Is '$this->_conn' property declared in the class or it is dynamic property?
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Object reference not set to an instance of an object err

Postby compucoder » November 7th, 2012, 2:25 pm

Sorry for such a long gap between replies. I moved on to another big project and I now getting back to using Phalanger.

This issue stil occurs. I have a Class and in the constructor I create the connection which works fine. I then try and save the connection in a private variable. Eg.

Code: Select all
Class foo() {
  private $_conn = null;

  public function _construct($host, $database, $username, $password) {
    $conn = @mysql_connect($host, $username, $password);
    mysql_select_db($database, $conn);
    $this->_conn = $conn; // dies here instantly with object error
  }

}


I verified that $conn is a valid resource and it can accept queries. I have tried changing the class var to public with no success. It just seems like you can't store a resource in a class variable; this unfortunately is crippling to me as this is my DB abstracyion layer - big job to change how this is done.
compucoder
 
Posts: 12
Joined: August 16th, 2012, 12:29 pm

Re: Object reference not set to an instance of an object err

Postby Jakub Misek » November 7th, 2012, 4:02 pm

Looks like $this is null ?

Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Object reference not set to an instance of an object err

Postby compucoder » November 7th, 2012, 4:15 pm

$this is actually a special varible which means this class or self. You use $this-> to reference properties and methods within the class.

Phalanger handles it just fine for all other cases. So if I had a property like: private $foovar = ''. I can reference that anywhere in the class like: $this->foovar = 'hi'

Here is a more complete example (which works fine in Phalanger)

Code: Select all
class Foo() {
  public $fooVar = '';

  public function __construct() {
    $this->fooVar = 'hi';
  }
}

$f = new Foo();
echo $f->fooVar; // I get Hi



This works great for everything except when using the mysql_connect() resource - this is where things go bad.
compucoder
 
Posts: 12
Joined: August 16th, 2012, 12:29 pm

Re: Object reference not set to an instance of an object err

Postby Jakub Misek » November 7th, 2012, 4:56 pm

I'm really aware of definition of $this :-D

This would be the only cause to get this exception at this line. ('cause $this really can be NULL in PHP ...) The other option would be, the debug information is incorrect, and you're getting the exception one line before.

Do you have whole stack trace of the exception?

Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Object reference not set to an instance of an object err

Postby compucoder » November 7th, 2012, 6:16 pm

I just recycled the app pool, made a change to the file and now it works. I am unsure what actually fixed this though. The change I made was to comment out mysql_charset() and then I put it back in - then everything works. Very bizarre.

I now got past the opening roadbloack and hit more errors. I will post new threads for the bugs I just found. They are Phalanger and mysql extension bugs I think.
compucoder
 
Posts: 12
Joined: August 16th, 2012, 12:29 pm

Re: Object reference not set to an instance of an object err

Postby Jakub Misek » November 7th, 2012, 6:50 pm

Thank you, I'm looking forward to them :)
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 19 guests

cron

User Control Panel

Login

Who is online

In total there are 19 users online :: 0 registered, 0 hidden and 19 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 19 guests