PDA

View Full Version : Cannot access a property or method of a null object reference



jcanistrum
August 9th, 2007, 06:41 AM
Hi All,

I´m stucked with "Cannot access a property or method of a null object reference"

I did a small test inside the IDE and it works fine



var _container:Sprite;
var holder:Sprite = new Sprite();
addChild( holder );

PainelNumeros( holder );
function PainelNumeros(mc:Sprite)
{
_container = new Sprite();
draw();
mc.addChild(_container);
}
function draw():void
{
var origX = 23;
var origY = 126.5;
var deltaX = 24.5;
var deltaY = 17.9;
var larguraBox = 17.8;
var alturaBox = 9.4;
var posX = origX;
var posY = origY;
var elemento = 0;
for (var i = 1; i <= 10; i++)
{
for (var j = 1; j <= 10; j++)
{
var nome = "numero" + elemento ;
_container.addChild( new SpriteBox() ).name = nome;
_container.getChildByName( nome ).x = posX;
_container.getChildByName( nome ).y = posY;
_container.getChildByName( nome ).width = larguraBox;
_container.getChildByName( nome ).height = alturaBox;
_container.getChildByName( nome ).alpha = 0.5;
_container.getChildByName( nome ).addEventListener(MouseEvent.CLICK, mouseClickHandler);
posX += deltaX;
elemento++;
}
posX = origX;
posY += deltaY;
}
}
function mouseClickHandler(event:MouseEvent)
{
trace( event.target.parent.name );
}
.................................................. ..............
package
{
import flash.display.*;
import flash.events.*;
public class SpriteBox extends Sprite
{
private var size:uint = 50 ;
private var status:Boolean;
private var bgColor:uint = 0xFFCC00;
public function SpriteBox()
{
var child:Sprite = new Sprite();
status = false;
draw(child);
addChild(child);
}
private function draw(sprite:Sprite):void
{
sprite.graphics.beginFill(bgColor);
sprite.graphics.drawRect(0, 0, size, size);
sprite.graphics.endFill();
}
}
}


Then I tryed to translate what I got through the IDE to a new class



package
{
import flash.display.*;
import flash.events.*;
public class PainelNumeros
{
private var _container:Sprite;
public function PainelNumeros(mc:Sprite)
{
var _container:Sprite = new Sprite();
draw();
mc.addChild(_container);
}
private function draw():void
{
var origX = 23;
var origY = 126.5;
var deltaX = 24.5;
var deltaY = 17.9;
var larguraBox = 17.8;
var alturaBox = 9.4;
var posX = origX;
var posY = origY;
var elemento = 0;
for (var i = 1; i <= 10; i++)
{
for (var j = 1; j <= 10; j++)
{
var marca:SpriteBox = new SpriteBox();
var nome = "numero" + elemento;
_container.addChild( new SpriteBox() ).name = nome;
_container.getChildByName( nome ).x = posX;
_container.getChildByName( nome ).y = posY;
_container.getChildByName( nome ).width = larguraBox;
_container.getChildByName( nome ).height = alturaBox;
_container.getChildByName( nome ).alpha = 0.5;
_container.getChildByName(nome).addEventListener(M ouseEvent.CLICK, mouseClickHandler);
posX += deltaX;
elemento++;
}
posX = origX;
posY += deltaY;
}
}
private function mouseClickHandler(event:MouseEvent)
{
trace( event.target.parent.name );
}
}
}


and tryed to instantiate it in the IDE



var holder:Sprite = new Sprite();
addChild( holder ) ;

var painel1:PainelNumeros = new PainelNumeros ( holder ) ;


Where I get the error, what am I doing wrong ?

thanks in advance

jcarlos

gligy
August 9th, 2007, 07:03 AM
oh man put some traces and you will find out where is your error :) It;s the best way

Dark Viper
August 9th, 2007, 07:18 AM
Run in debug mode, it will tell you exactly which line is causing the error

iamdesign
August 9th, 2007, 08:43 AM
mc.addChild(_container);where do you create mc?

jcanistrum
August 9th, 2007, 08:44 AM
exactly here


_container.addChild( new SpriteBox() ).name = nome;


Run in debug mode, it will tell you exactly which line is causing the error

Dutchy
August 9th, 2007, 09:21 AM
Hi,

replace:


var marca:SpriteBox = new SpriteBox();
var nome = "numero" + elemento;
_container.addChild( new SpriteBox() ).name = nome;
_container.getChildByName( nome ).x = posX;
_container.getChildByName( nome ).y = posY;
_container.getChildByName( nome ).width = larguraBox;
_container.getChildByName( nome ).height = alturaBox;
_container.getChildByName( nome ).alpha = 0.5;


with:


var marca:SpriteBox = new SpriteBox();
marca.name = "numero" + elemento;
marca.x = posX;
marca.y = posY;
marca.width = larguraBox;
marca.height = alturaBox;
marca.alpha = 0.5;
_container.addChild(marca);


This is simpler.... and make the _container Sprite a 'public' variable instead of a 'private' variable

Dutchy

jcanistrum
August 9th, 2007, 09:27 AM
I´m passing this from main timeline , but I think here is where I´m missing from something as to compare with AS2



mc.addChild(_container);where do you create mc?

jcanistrum
August 9th, 2007, 09:44 AM
I´ve solved, thanks ...

was exactly that .... was a primary mistake -)

the correct way ( certainly not the only one ) is to make PainelNumeros do extend sprite and make PainelNumeros to be the Document class for the .fla I was testing.



I´m passing this from main timeline , but I think here is where I´m missing from something as to compare with AS2