Compiling to HTML5: TypeError: ApplicationMain.config is null

I converted my flash code to OpenFl, and it now run nicely in the Flash target. So the next step has been to make it compile in HTML5 as well.

However I now have some errors I do not know what to do with:
TypeError: ApplicationMain.config is null
pointing to these lines:
23631 "ApplicationMain.config.background = color;"
2390 “var assetsPrefix = ApplicationMain.config.assetsPrefix;”

I also have several of these errors:
SyntaxError: unreachable code after return statement

This are the libraries I am currently using:
openfl [3.2.2]
lime [2.5.2]
swf [1.8.9]

I would be happy to get help and tips on how to handle this.
If there is something in my code that is causing this I would also be happy to get tips on how to debug that, since I don’t get so much information from the browser console.

Hi Sara,

I don’t know if this is the case,
But I got this error several times before, and on all case closing all windows and restarting the IDE solved the problem.

Thanks for the tip, I tried it (restarted my PC since I am running it through the command line), but unfortunately it didn’t solve it for me.

Are you building using the openfl command, or are you using an HXML file manually?

I would recommend testing to see if some of the OpenFL samples are working for you:

openfl create PiratePig
cd PiratePig
openfl test html5

I also made a small fix that may help with building without tools in this case :slight_smile:

1 Like

I am using the openfl command to build it. And PiratePig works, so there is probably something wrong in my code then I assume.

Is there any tips on what could help to debug this?
Or any pointers to things that often causes these kind of problems?
Or anything else that could help making HTML5 debugging easier :smiley:

Do you use any custom template files, such as overriding ApplicationMain with your own copy?

I had a look through it, but no, I do not override any template files :confused:

Does it work if you upgrade to the latest versions?

Any update on this? I’m experiencing very similar symptoms:

  • I ported my AS3 game to Haxe and OpenFL over the weekend.
  • Flash target runs flawlessly.
  • HTML5 build compiles without a hitch, but does nothing.

Firefox’s console reports these errors:

TypeError: openfl_Lib.application is null MyGame.js:2224:2
TypeError: ApplicationMain.config is null MyGame.js:19937:2

Corresponding respectively to these blocks of code:

var openfl_net_SharedObject = function() {
    openfl_events_EventDispatcher.call(this);
    this.client = this;
    this.objectEncoding = 3;
    // Line 2224
    openfl_Lib.application.onExit.add($bind(this,this.application_onExit));
};

and

lime_system_System.embed = $hx_exports.lime.embed = function(element,width,height,background,assetsPrefix) {
    var htmlElement = null;
    if(typeof(element) == "string") htmlElement = window.document.getElementById(js_Boot.__cast(element , String)); else if(element == null) htmlElement = window.document.createElement("div"); else htmlElement = element;
    var color = null;
    if(background != null) {
        background = StringTools.replace(background,"#","");
        if(background.indexOf("0x") > -1) color = Std.parseInt(background); else color = Std.parseInt("0x" + background);
    }
    if(width == null) width = 0;
    if(height == null) height = 0;
    // Line 19937
    ApplicationMain.config.windows[0].background = color;
    ApplicationMain.config.windows[0].element = htmlElement;
    ApplicationMain.config.windows[0].width = width;
    ApplicationMain.config.windows[0].height = height;
    ApplicationMain.config.assetsPrefix = assetsPrefix;
    ApplicationMain.create();
};

I just installed everything over the weekend for the first time, so I should have just about the most up-to-date versions of everything:

  • OpenFL: 3.3.6
  • Lime: 2.6.6
  • Haxe: 3.2.0

I’m using FlashDevelop 5.0.1.3, I closed the IDE, then cleaned and rebuilt the project, with the same results. Smaller test projects work fine, so I assume that I too am doing something invalid in my code. I don’t believe I’m doing anything exotic, as I haven’t learned much about Haxe or OpenFL yet.

Have you overridden any of the default file templates?

What happens in a different browser?

I’ve not overridden any default file templates.

I tried in Chrome and IE11 with similar results.

Exploring it a little bit, the first error was in code related to SharedObjects, so I commented out any reference to openfl.net.SharedObject in my code, and it looks like I was able to overcome whatever was happening. Is it possible there are bugs related to how these are handled in the HTML5 code? (I assume there’s a compatibility layer using cookies or something for these? I know very little about the HTML5 spec.)

@Sara, if you’re still around, does this solve your issue as well? Has anyone else had issues with openfl.net.SharedObject in HTML5 targets?

Could you post a build of your project somewhere that I could view? If its private, you could send a private message, but this might help to see it in action myself :slight_smile:

I was about to send a PM, but then I found out the issue was related to the design pattern I was using for singletons. (A pattern that I admittedly was not fond of.) Here’s a small sample program that reproduces the issue:

package com.skoobalon.test;

import com.skoobalon.test.Main.MySingleton;
import openfl.display.Sprite;
import openfl.Lib;
import openfl.net.SharedObject;
import openfl.errors.Error;

class Main extends Sprite {
    public function new() {
        super();
    }
}

class MySingleton {
    public static var instance(default, null):MySingleton = new MySingleton();
    private var _so:SharedObject;
    public function new() {
        if (instance != null) {
            throw new Error("Singleton class");
        }
        trace("Making a new singleton");
        _so = SharedObject.getLocal("foo");
    }
}

This code compiles fine and works in the Flash player, but does not work for HTML5. So I would surmise that the bug is in the generated HTML5 code. And I’m sure that a workaround can be concocted with little effort on my end. I fear that I don’t have time to investigate the OpenFL code myself to come up with a solution to this problem.

That’s probably it. Some things won’t be available yet when new MySingleton() gets called.

Try using this instead.