Page 1 of 1

Object reference not set to an instance of an object error

PostPosted: August 16th, 2012, 12:36 pm
by compucoder
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.

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

PostPosted: August 16th, 2012, 2:10 pm
by Jakub Misek
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?

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

PostPosted: November 7th, 2012, 2:25 pm
by compucoder
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.

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

PostPosted: November 7th, 2012, 4:02 pm
by Jakub Misek
Looks like $this is null ?

Thanks,

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

PostPosted: November 7th, 2012, 4:15 pm
by compucoder
$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.

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

PostPosted: November 7th, 2012, 4:56 pm
by Jakub Misek
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,

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

PostPosted: November 7th, 2012, 6:16 pm
by compucoder
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.

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

PostPosted: November 7th, 2012, 6:50 pm
by Jakub Misek
Thank you, I'm looking forward to them :)