Tile.fromColor(0xffffff) and similiar functions gives a null access exception

Heaps is not really working at all for me at all. Various functions like DefaultFont.get() and Tile.fromColor() gets a error relating to memory.

This code:

class Main {
    static function main() {
      app = new App();
      Tile.fromColor(0xffffff);
    }
}

throws an exception with this callstack

CallStack:
h3d.mat.Texture.alloc (c:\HaxeToolkit\haxe\lib\heaps\1,9,1\h3d\mat\Texture.hx:122)
h3d.mat.Texture.new (c:\HaxeToolkit\haxe\lib\heaps\1,9,1\h3d\mat\Texture.hx:113)
h3d.mat.Texture.fromColor (c:\HaxeToolkit\haxe\lib\heaps\1,9,1\h3d\mat\Texture.hx:435)
h2d.Tile.fromColor (c:\HaxeToolkit\haxe\lib\heaps\1,9,1\h2d\Tile.hx:357)
Main.main (c:\Users\User\Desktop\Project\src\Main.hx:16)

The code that errors is in the class h3d.mat.Texture

class Texture {
	public function alloc() {
		if( t == null )
			mem.allocTexture(this); // <-- Exception has occurred. Null access
	}
}
Debugger values:
this:
    t: null
    mem: null

I’ve tried making a new empty project and it doesn’t work and neither does reinstalling heaps or haxe. Some help would be appreciated. Thanks in advance

Hi there!
It’s pleasant to see a question that is longer than one phrase, however it seems you either spare much from your actual code or have not really a clue what you’re trying to code here (e.g. you cannot save App in app without using var, this is very basic Haxe code etc.).

Have you run the Heaps hello world samples before trying to write custom code?
(Also sometimes there are troubles only mentioned on github. E.g. at the moment there seems to be a problem when using the latest VSCode with Hashlink so you need to downgrade… before that there was an issue with the latest Haxe/Heaps version etc. So new devs cannot know those issues :confused:)

I don’t know what direction you come from, but here are some basic hints in the unofficial FAQ by Yanris. You get the idea Heaps is an engine most devs build an engine upon (e.g. Deepnight, Yanrishatum, AveNyrh, jefvel, Xusk and many more etc.)


Anyway this would be your code:


import hxd.res.DefaultFont;

class DisplayTileFromColor extends hxd.App {

    static var app : DisplayTileFromColor; // you don't need this in this sample

    var bmp : h2d.Bitmap; // save a reference so we can move it in update function

    override function init() {
        var tile = h2d.Tile.fromColor(0x6F00FF, 50, 50);
        // at first tile will only exists in GPU memory
        // so we need to do something based upon it being loaded in GPU
        // create a Bitmap object, which will display the tile
		// and will be added to our 2D scene (s2d)
        bmp = new h2d.Bitmap(tile, s2d);
        bmp.setPosition( s2d.width/2, 80 );
        var info = new h2d.Text( DefaultFont.get(), bmp );
        info.text = "Hi! :)";
    }

    override function update(dt:Float) {
        bmp.y += 1;
    }

    static function main() {
        trace("Hello world!");
        app = new DisplayTileFromColor(); // "app = " is not needed
    }
}

And your .hxml-file:

-lib heaps
-lib hlsdl

-main DisplayTileFromColor

-D windowSize=1280x720
-D windowTitle=Your app´s title in window

-hl app.hl

And you can visit us in :point_right: Haxe#Heaps on Discord. :slight_smile:

I forgot to include the static var for app inside main so that was kind of confusing. I copied your code and it ran fine so I played around with it a little and found the problem which was that I tried to these things inside main() which isn’t allowed. It is resolved now. Thanks for your help!

1 Like