What am I doing wrong?

127 views
Skip to first unread message

Mandemon

unread,
Jul 5, 2013, 6:24:57 AM7/5/13
to haxe...@googlegroups.com
Hello. I am new to Haxe and OpenFL. I have very limited experience with AS3 and most of my programming experience comes from C++ and Java.

However, I looked at Haxe and found it interesting and decided to give it a try. I am using FlashDevelop, the latest version with support for OpenFL.

Anyway, long story short. I  tried to have an array/vector with members of class Characters. I keep getting" TypeError: Error #1009: Cannot access a property or method of a null object reference." constantly. I have been sticking to Flash output for now.

Here is the problem: I have following Array:

var characters_:Array<Character>;

And my Character class looks like this:

package ;

class Character
{


 
public var name:String;
 
 
public function new(_name:String)
 
{
 trace
("I am a new character");
 name
= _name;
 trace
("My name is " + name);
 
 
}
}

Now, the problem arises here:

 //Test character creation
 
 trace
("Creating a new character");
 
var test:Character = new Character("Tom"); //Works correctly
 trace
("Creating another new character");
 characters_
.push(new Character("Bill") ); //Prints out correctly, but gives an error
 trace
("A new character was created successfully");//Does not print

Have I understood something wrong about Haxe and Arrays?

Jason O'Neil

unread,
Jul 5, 2013, 6:38:48 AM7/5/13
to haxe...@googlegroups.com

> var characters_:Array<Character>;

Should be

> var characters_:Array<Character> = [];

Basically when you declare a variable in Haxe, you need to initialize it or else it will be null.

If that's not the issue maybe post a more complete code sample :)

Good luck!
Jason

> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "Haxe" group.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

sergey miryanov

unread,
Jul 5, 2013, 6:38:53 AM7/5/13
to haxe...@googlegroups.com

I bet you forgot to create characters array :)

05.07.2013 16:25 пользователь "Mandemon" <mande...@gmail.com> написал:

Mandemon

unread,
Jul 5, 2013, 7:20:19 AM7/5/13
to haxe...@googlegroups.com
Unfortunately, that didn't work. Now it refuses to run at all! I get following error:

src/Main.hx:26: characters 1-39 : Variable initialization must be a constant value

Full code of Main.hx is:

package ;


import flash.display.Sprite;
import flash.events.Event;
import flash.Lib;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;


//My own classes
import Character;


/**
 * ...
 * @author Mandemon
 */



enum STATUS { MENU; INGAME; PAUSED; }
 
class Main extends Sprite
{
 
var inited:Bool;
 
var state_:STATUS;
 
var characters_:Array<Character> = []; //This one gives the error, Variable initialization must be a constant value
 
 
/* ENTRY POINT */
 
 
function resize(e)
 
{
 
if (!inited) init();
 
// else (resize or orientation change)
 
}
 
 
function init()
 
{
 
if (inited) return;
 inited
= true;
 
 
// (your code here)
 state_
= MENU;
 trace
(state_);

 
 
//Test character creation
 
 trace
("Creating a new character");
 
var test:Character = new Character("Tom");

 trace
("Creating another new character");
 characters_
.push(new Character("Bill") );

 trace
("A new character was created successfully");

 
//var stats:Array<Int> = characters_[0].getStats();
 
 
 
// Stage:
 
// stage.stageWidth x stage.stageHeight @ stage.dpiScale
 
 
// Assets:
 
// nme.Assets.getBitmapData("img/assetname.jpg");
 
}


 
/* SETUP */


 
public function new()
 
{
 
super();
 addEventListener
(Event.ADDED_TO_STAGE, added);
 
}


 
function added(e)
 
{
 removeEventListener
(Event.ADDED_TO_STAGE, added);
 stage
.addEventListener(Event.RESIZE, resize);
 
#if ios
 haxe
.Timer.delay(init, 100); // iOS 6
 
#else
 init
();
 
#end
 
}
 
 
public static function main()
 
{
 
// static entry point
 
Lib.current.stage.align = flash.display.StageAlign.TOP_LEFT;
 
Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
 
Lib.current.addChild(new Main());
 
}
}


As for Character.hx

Jason O'Neil

unread,
Jul 5, 2013, 7:33:28 AM7/5/13
to haxe...@googlegroups.com
You need to initialize your characters_, but you can't initialize an array from it's declaration in a class field - you'll need to do that either in the constructor, or in init().  The rules about what you can initialize variables with probably aren't worth worrying about for now, just do it from inside of a function and you should be okay.

See the code below, changed lines in bold.

Jason

On Fri, Jul 5, 2013 at 7:20 PM, Mandemon <mande...@gmail.com> wrote:
package ;


import flash.display.Sprite;
import flash.events.Event;
import flash.Lib;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;


//My own classes
import Character;


/**
 * ...
 * @author Mandemon
 */



enum STATUS { MENU; INGAME; PAUSED; }
 
class Main extends Sprite
{
 
var inited:Bool;
 
var state_:STATUS;

 
var characters_:Array<Character>; //Don't initialize here, Haxe can't initialize class instance variables with anything other than int/string/constants.
 
 
/* ENTRY POINT */

 
 
function resize(e)
 
{
 
if (!inited) init();
 
// else (resize or orientation change)
 
}
 
 
function init()
 
{
 
if (inited) return;
 inited
= true;

 // (initialize the characters array)
 characters_ = [];

Mandemon

unread,
Jul 5, 2013, 7:40:40 AM7/5/13
to haxe...@googlegroups.com
That worked! Thanks. I initialized the array before starting to create a new characters and it worked.

Heinz Hölzer

unread,
Jul 5, 2013, 7:42:34 AM7/5/13
to haxe...@googlegroups.com
just to make it clear, you have to initialize it before you push elements in it or use any other method on your _characters array. That's the important point.
Reply all
Reply to author
Forward
0 new messages