AS3: Null object reference when attempting to check if it exists? - actionscript-3

I have a weird problem. An Object is being passed to my function, and some parameters are optional, so naturally I would check to see if they are there, and if not, do nothing.
However, I'm getting a null reference error (#1009) when I'm just checking it. Here's the sample:
public function parseObject(params:Object) {
if (params.optionalParam)
trace("Got Optional Parameter!");
}
The error is returned on the line with the if statement. Changing it to check for null (if (params.optionalParam == null)) doesn't work either. The players seems to just give up if an object doesn't exist.
Is there any logical reason for this to happen? Or is it some weird bug that has just surfaced?
Thanks in Advance,
-Esa

If your params object is null, then you will get a null reference error when trying to access its' optionalParam property.
Try something like:
if (params == null)
{ trace("params is null!"); }
else if (params.optionalParam != null)
{ trace("Got optional parameter!"); }

Related

no implicit conversion between string and int in razor view javascript area

<script type="text/javascript">
var destinationId = #(Model.DestinationId == 0?"":Model.DestinationId);
</script>
I want to output "" if Model.DestinationId is 0 else display Model.DestinationId
Because your C# code is trying to return a string when the if condition yields true and returns an int(value of DestinationId) when the condition expression returns false. The compiler is telling you that it is not valid! You need to return the same type from both the cases.
To fix the issue, return the same type from both the cases. You can use ToString() method on the int type property so that your ternary expression will always return the same type (string)
var destinationId = #(Model.DestinationId == 0 ? "" : Model.DestinationId.ToString());
Although the above will fix your compiler error, it might not be giving what you are after. The above code will render something like this when your DestinationId property value is 0.
var destinationId = ;
Which is going to give you a script error
Uncaught SyntaxError: Unexpected token ;
and when DestinationId property has a non zero value, for example, 10.
var destinationId = 10;
There are multiple ways to solve the issue. One simple approach is to replace the empty string with something JavaScript understands. Here in the below sample, I am rendering null as the value
var destinationId = #(Model.DestinationId == 0 ? "null" : Model.DestinationId.ToString());
if (destinationId===null) {
console.log('value does not exist');
}
else {
console.log('value is '+ destinationId);
}
Another option is to simply read the DestinationId property value and check for 0 in your JavaScript code as needed.
Another option is to wrap your entire C# expression in single or double quotes. But then again you number value will be represented as a string :(, which is not good.
I suggest you to use the correct type(even in JavaScript)

missing value from json object when writing if condition it's througing an error

my JSON Object is varies based on some conditins. when i am trying to access the value from json object when it is not present it's througing an error message saying that value is not defined. How to avoid such kind of situations??
IntentSlots: '{"cnumberslot":{"name":"cnumberslot","value":"C186206"}}' - condition works perfectly.
IntentSlots: '{"cnumberslot":{"name":"cnumberslot"}}' } - throws an error.
my condition is like below,
if(IntentSlots.cnumberslot.value !== ""){
}
Based on the condition if the property is not available add an empty value to that property because if u leave it empty it will throw that error.
or use different if condition
if(IntentSlots.hasOwnProperty('value') && IntentSlots.cnumberslot.value !== ""){ }

Having trouble checking for null condition in coffee script

I have a coffee script with the following
#update_states = (countryElt, stateElt, callbackFn) ->
…
if callbackFn != null
callbackFn()
The problem is, even when there is no “callbackFn” argument passed to the function, the “if” block is getting executed. What is the proper way to check if the argument is not null (i.e. is a function passed to the function)?
Code if callbackFn != null is converted to if(callbackFn !== null). If You don't pass the callbackFn argument, callbackFn = undefined. undefined !== null.
Correct way to do this in coffeescript is:
if callbackFn? then callbackFn()
Read more about existential operators

Using variable within a literal

In my code I need to declare notationArr1 but I'm getting this error: Error #1010: A term is undefined and has no properties.
if ((notationArr[1].length == 2) && ((notationArr[1].charCodeAt(0) >= 97) && notationArr[1].charCodeAt(0) <= 104) && ((notationArr[1].charCodeAt(1) >= 49) && notationArr[1].charCodeAt(1) <= 56)) {
if (pieces.d3.man == "") {
pieces.notationArr[1].man.y = pieces.d4.y;
}
}
Here, pieces is an object.
Edit: More code: http://sudrap.org/paste/text/44915/
One of the many variables in your little code piece was not properly declared and/or initialized. You can only access properties or methods (every time you write something.something, that's the part after the .) on existing Objects, but not if the variable you are trying to access contains null.
EDIT
Having read your longer code piece, there could be several null variables, but your problem is probably what #AsTheWormTurns mentioned in his comment above:
pieces.notationArr[1].man
will try to access an array called notationArr that is a member of pieces, instead of using an evaluation of the content of notationArr[1] to find out which member of pieces to access. It should be:
pieces[notationArr[1]].man

how do I indicate an unset default parameter of type Number in Flex3?

for String and Object type, I can set the default parameter to null to indicate that it was not set by the caller. Is there a mechanism in flex3 to do the same for the Number type?
So for instance:
public function myMethod( stringVar:String=null, ObjectVar:Object=null, numberVar:Number )
{
...
}
I could do the following, but it just feels ugly
public function myMethod( numberVarObj:Object=null )
{
var numberVarSet:Boolean=true;
if( numberVarObj == null ) {
numberVarSet = false;
}
and then everywhere I want to use numberVar I can check for numberVarSet and cast as a Number.
I suppose you could always try:
var numberVar:* = null;
And then set it to a number when you want . . . It would be nice to have a solution that is strongly typed though.
Another option, as specified in Adobe's Docs (scroll down to default values), would be to treat the value NaN as null. However, if your data has ANY chance of containing a NaN value, this is a horrible idea.
I'd recommend the "ugly" solution you have, but if you really want another option you can use NaN and then use isNaN(num) to check the value.