Object reference not set to an instance of an object

Apr 11, 2011 at 2:27 PM
Edited Apr 11, 2011 at 2:34 PM

Hi,

I am writing to you as I am facing an Exception while minifying some javascript. (I am using Ajax Minifier 4.15)

This is happening even before my attempt at pruning some console statements (which I did not see how to do it in the documentation, so you might have some advice in there)

I encountered the issue with a line of code like this : a = b[null]; as soon as I call Parser.parse(cs).

I had not yet seen such a key and hence went to ECMA-262 and saw it as something valid.

Try minifying (parsing at least) the following code:

 

a = { toto:"titi" };
a[null] = "tata"; // !_!
a[undefined] = "tete"; // ?_?
a[true] = "tutu";
a[false] = "tyty";
a[3.45] = "numeral 3.45";

alert(a["toto"]);

alert(a[null]); // O_o
alert(a[undefined]); // ^-^
alert(a[1 == 1]);
alert(a[1 == 0]);
alert(a[3.45]);

alert(a["null"]);
alert(a["undefined"]);
alert(a["true"]);
alert(a["false"]);
alert(a["3.45"]);

alert(a["titi"]);

 

it raises an Exception with a[null] whereas loading this under IE/FF/Chrome is just fine.

I am pasting here my "minifying code" in case I am not doing something right...

 

// Configure Javascript Shrinking Options
cs = new CodeSettings();
cs.LocalRenaming = LocalRenaming.CrunchAll;
cs.MacSafariQuirks = true;
cs.OutputMode = OutputMode.MultipleLines;
cs.IndentSize = 0;
cs.StripDebugStatements = true;
cs.CollapseToLiteral = true;

private String MinifyJavaScript(String text)
{
	JSParser parser = new JSParser(text, null);

	try
	{
		Block scriptBlock = parser.Parse(cs);
		// parse the input  
		parseNodes(scriptBlock.Children);

		return scriptBlock.ToCode();
	}
	catch (JScriptException e)
	{
		// other error handling 
		Console.WriteLine("MinifyJavaScript got a JScriptException : " + e.Message);
	}
	catch (Exception e)
	{
		Console.WriteLine("MinifyJavaScript got an Exception : " + e.Message);
	}

	return "";
}

private void parseNodes(IEnumerable<AstNode> ie)
{
	foreach (AstNode node in ie)
	{
		if (node is CallNode && node.Context.Code.StartsWith("console."))
		{
			if (!node.Context.Code.StartsWith("console.error") && !node.Context.Code.StartsWith("console.warn"))
				node.Parent.ReplaceChild(node, null);
		}
		else
		{
			parseNodes(node.Children);
		}
	}
}

 

I thank you in advance with all the help you could provide me with.

I am currently using Ajax Minifier to "build layers" ala Dojo buildscript and it's quite useful.

Thanks and Regards

-stef

Coordinator
Apr 18, 2011 at 9:35 PM

[Sorry for the delay; been in Hawaii the last two weeks]

I can repro the error -- I'll get a fixed build out for you this afternoon.

Coordinator
Apr 18, 2011 at 10:20 PM

Okay, this should be fixed now in version 4.16. Great catch -- thanks for reporting it!

Apr 21, 2011 at 12:47 PM

Hi,

i just tested it, it works like a charm now ! 

Thanks for the quick fix, no worries for the delay, time in Hawaii has to be enjoyed ;-)

-stef