Object become null during conversion (with or without cast) on Android

I’ve got a really strange issue in my code during Android tests.

        var sellingItemsMap:Dynamic = DataStorage.getInstance().getValue(GameAttributes.SESSION_SELLINGITEMS);
	if (sellingItemsMap == null) {
		Logger.debug("sellingItemsMap is null");
	} else {
		Logger.debug("sellingItemsMap NOT is null");
	Logger.debug("iterableSellingItemsMap: "+iterableSellingItemsMap);
	var iterableSellingItemsMap:Map<Int,SellingItem> = cast(sellingItemsMap);
	if (iterableSellingItemsMap == null) {
		Logger.debug("iterableSellingItemsMap is null");
	} else {
		Logger.debug("iterableSellingItemsMap NOT is null");
	Logger.debug("iterableSellingItemsMap: "+iterableSellingItemsMap); <- Row 1026

Here you can see the log:

I/trace   ( 3896): ShopSceneBuilder.hx:1015: sellingItemsMap NOT is null
I/trace   ( 3896): ShopSceneBuilder.hx:1017: sellingItemsMap: { 4 => SellingItem, 2 => SellingItem, 3 => SellingItem, 1 => SellingItem}
I/trace   ( 3896): ShopSceneBuilder.hx:1021: iterableSellingItemsMap is null
I/trace   ( 3896): Lib.hx:360: Null Object Reference
I/trace   ( 3896): Called from redevogames.redevoclient.redemption.gui.builders.scenes.shop.ShopSceneBuilder.initSellingItemsData (redevogames/redevoclient/redemption/gui/builders/scenes/shop/ShopScen
eBuilder.hx line 1026)

The most strange behavior is a strange crash on last row (1026) of code during logging. Seems there’s something really wrong: Logger cannot log a null value, I suppose is not really null as showed in logs.
Anyway sellingItemsMap and iterableSellingItemsMap could be cointans same data but, the first has data, second is null.
Anyone can suggest me an alternative, or right, way to avoid crash and nullifying?

Thanks in advance,

Is sellingItemsMap actually a map, or a dynamic object with keys? Would Reflect.fields be more appropriate to iterate over it?

Thank for reply Joshua.
I forgot to say no issues targeting flash.
I just discover there was a declaration mistake when the Map is created before storing in DataStorage: sellingItemsMap is really a Map, perhaps not Map<Int, SellingItem> but a Map<String, SellingItem>. Flash seems more flexible than Android and overlook this error. Android punish me with 3 hours of extra work.

Thanks for advice but after correction code works properly.
Thanks again, David.

Make sure you enclose your angle brackets in back-quotes (`), or they’ll be treated as html tags rather than text.

Can you explain me better? I never had issues like that.

Oh, he means in your post here on the forums. I edited your post and fixed it.

With back ticks:

Map<Int, SellingItem> but a Map<String, SellingItem>


Map<Int, SellingItem> but a Map<String, SellingItem>

…you see that it edits out the angle brackets :slight_smile:

Edit: Joshua beat me to it. Oh well.

On the forums, if you type something in angle brackets, it interprets it an HTML tag. This is useful for actual HTML tags: <del>strike through text</del> becomes strike through text.

However, it interprets everything as an HTML tag, even if it isn’t. This means that when you type “perhaps not Map<Int, SellingItem> but a Map<String, SellingItem>,” it appears as “perhaps not Map<Int, SellingItem> but a Map<String, SellingItem>.”

Back-quotes tell the forums to display your code verbatim, rather than trying to render it as HTML. This means you can type `Map<String, SellingItem>` to get Map<String, SellingItem>.

Understood thanks! Previously I was confused because I think player_03 is writing about code :smiley:
Thanks a again.